Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * GPIO driver for virtio-based virtual GPIO controllers
0004  *
0005  * Copyright (C) 2021 metux IT consult
0006  * Enrico Weigelt, metux IT consult <info@metux.net>
0007  *
0008  * Copyright (C) 2021 Linaro.
0009  * Viresh Kumar <viresh.kumar@linaro.org>
0010  */
0011 
0012 #include <linux/completion.h>
0013 #include <linux/err.h>
0014 #include <linux/gpio/driver.h>
0015 #include <linux/io.h>
0016 #include <linux/kernel.h>
0017 #include <linux/module.h>
0018 #include <linux/mutex.h>
0019 #include <linux/spinlock.h>
0020 #include <linux/virtio_config.h>
0021 #include <uapi/linux/virtio_gpio.h>
0022 #include <uapi/linux/virtio_ids.h>
0023 
0024 struct virtio_gpio_line {
0025     struct mutex lock; /* Protects line operation */
0026     struct completion completion;
0027     struct virtio_gpio_request req ____cacheline_aligned;
0028     struct virtio_gpio_response res ____cacheline_aligned;
0029     unsigned int rxlen;
0030 };
0031 
0032 struct vgpio_irq_line {
0033     u8 type;
0034     bool disabled;
0035     bool masked;
0036     bool queued;
0037     bool update_pending;
0038     bool queue_pending;
0039 
0040     struct virtio_gpio_irq_request ireq ____cacheline_aligned;
0041     struct virtio_gpio_irq_response ires ____cacheline_aligned;
0042 };
0043 
0044 struct virtio_gpio {
0045     struct virtio_device *vdev;
0046     struct mutex lock; /* Protects virtqueue operation */
0047     struct gpio_chip gc;
0048     struct virtio_gpio_line *lines;
0049     struct virtqueue *request_vq;
0050 
0051     /* irq support */
0052     struct virtqueue *event_vq;
0053     struct mutex irq_lock; /* Protects irq operation */
0054     raw_spinlock_t eventq_lock; /* Protects queuing of the buffer */
0055     struct vgpio_irq_line *irq_lines;
0056 };
0057 
0058 static int _virtio_gpio_req(struct virtio_gpio *vgpio, u16 type, u16 gpio,
0059                 u8 txvalue, u8 *rxvalue, void *response, u32 rxlen)
0060 {
0061     struct virtio_gpio_line *line = &vgpio->lines[gpio];
0062     struct virtio_gpio_request *req = &line->req;
0063     struct virtio_gpio_response *res = response;
0064     struct scatterlist *sgs[2], req_sg, res_sg;
0065     struct device *dev = &vgpio->vdev->dev;
0066     int ret;
0067 
0068     /*
0069      * Prevent concurrent requests for the same line since we have
0070      * pre-allocated request/response buffers for each GPIO line. Moreover
0071      * Linux always accesses a GPIO line sequentially, so this locking shall
0072      * always go through without any delays.
0073      */
0074     mutex_lock(&line->lock);
0075 
0076     req->type = cpu_to_le16(type);
0077     req->gpio = cpu_to_le16(gpio);
0078     req->value = cpu_to_le32(txvalue);
0079 
0080     sg_init_one(&req_sg, req, sizeof(*req));
0081     sg_init_one(&res_sg, res, rxlen);
0082     sgs[0] = &req_sg;
0083     sgs[1] = &res_sg;
0084 
0085     line->rxlen = 0;
0086     reinit_completion(&line->completion);
0087 
0088     /*
0089      * Virtqueue callers need to ensure they don't call its APIs with other
0090      * virtqueue operations at the same time.
0091      */
0092     mutex_lock(&vgpio->lock);
0093     ret = virtqueue_add_sgs(vgpio->request_vq, sgs, 1, 1, line, GFP_KERNEL);
0094     if (ret) {
0095         dev_err(dev, "failed to add request to vq\n");
0096         mutex_unlock(&vgpio->lock);
0097         goto out;
0098     }
0099 
0100     virtqueue_kick(vgpio->request_vq);
0101     mutex_unlock(&vgpio->lock);
0102 
0103     wait_for_completion(&line->completion);
0104 
0105     if (unlikely(res->status != VIRTIO_GPIO_STATUS_OK)) {
0106         dev_err(dev, "GPIO request failed: %d\n", gpio);
0107         ret = -EINVAL;
0108         goto out;
0109     }
0110 
0111     if (unlikely(line->rxlen != rxlen)) {
0112         dev_err(dev, "GPIO operation returned incorrect len (%u : %u)\n",
0113             rxlen, line->rxlen);
0114         ret = -EINVAL;
0115         goto out;
0116     }
0117 
0118     if (rxvalue)
0119         *rxvalue = res->value;
0120 
0121 out:
0122     mutex_unlock(&line->lock);
0123     return ret;
0124 }
0125 
0126 static int virtio_gpio_req(struct virtio_gpio *vgpio, u16 type, u16 gpio,
0127                u8 txvalue, u8 *rxvalue)
0128 {
0129     struct virtio_gpio_line *line = &vgpio->lines[gpio];
0130     struct virtio_gpio_response *res = &line->res;
0131 
0132     return _virtio_gpio_req(vgpio, type, gpio, txvalue, rxvalue, res,
0133                 sizeof(*res));
0134 }
0135 
0136 static void virtio_gpio_free(struct gpio_chip *gc, unsigned int gpio)
0137 {
0138     struct virtio_gpio *vgpio = gpiochip_get_data(gc);
0139 
0140     virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_DIRECTION, gpio,
0141             VIRTIO_GPIO_DIRECTION_NONE, NULL);
0142 }
0143 
0144 static int virtio_gpio_get_direction(struct gpio_chip *gc, unsigned int gpio)
0145 {
0146     struct virtio_gpio *vgpio = gpiochip_get_data(gc);
0147     u8 direction;
0148     int ret;
0149 
0150     ret = virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_GET_DIRECTION, gpio, 0,
0151                   &direction);
0152     if (ret)
0153         return ret;
0154 
0155     switch (direction) {
0156     case VIRTIO_GPIO_DIRECTION_IN:
0157         return GPIO_LINE_DIRECTION_IN;
0158     case VIRTIO_GPIO_DIRECTION_OUT:
0159         return GPIO_LINE_DIRECTION_OUT;
0160     default:
0161         return -EINVAL;
0162     }
0163 }
0164 
0165 static int virtio_gpio_direction_input(struct gpio_chip *gc, unsigned int gpio)
0166 {
0167     struct virtio_gpio *vgpio = gpiochip_get_data(gc);
0168 
0169     return virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_DIRECTION, gpio,
0170                    VIRTIO_GPIO_DIRECTION_IN, NULL);
0171 }
0172 
0173 static int virtio_gpio_direction_output(struct gpio_chip *gc, unsigned int gpio,
0174                     int value)
0175 {
0176     struct virtio_gpio *vgpio = gpiochip_get_data(gc);
0177     int ret;
0178 
0179     ret = virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_VALUE, gpio, value, NULL);
0180     if (ret)
0181         return ret;
0182 
0183     return virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_DIRECTION, gpio,
0184                    VIRTIO_GPIO_DIRECTION_OUT, NULL);
0185 }
0186 
0187 static int virtio_gpio_get(struct gpio_chip *gc, unsigned int gpio)
0188 {
0189     struct virtio_gpio *vgpio = gpiochip_get_data(gc);
0190     u8 value;
0191     int ret;
0192 
0193     ret = virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_GET_VALUE, gpio, 0, &value);
0194     return ret ? ret : value;
0195 }
0196 
0197 static void virtio_gpio_set(struct gpio_chip *gc, unsigned int gpio, int value)
0198 {
0199     struct virtio_gpio *vgpio = gpiochip_get_data(gc);
0200 
0201     virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_VALUE, gpio, value, NULL);
0202 }
0203 
0204 /* Interrupt handling */
0205 static void virtio_gpio_irq_prepare(struct virtio_gpio *vgpio, u16 gpio)
0206 {
0207     struct vgpio_irq_line *irq_line = &vgpio->irq_lines[gpio];
0208     struct virtio_gpio_irq_request *ireq = &irq_line->ireq;
0209     struct virtio_gpio_irq_response *ires = &irq_line->ires;
0210     struct scatterlist *sgs[2], req_sg, res_sg;
0211     int ret;
0212 
0213     if (WARN_ON(irq_line->queued || irq_line->masked || irq_line->disabled))
0214         return;
0215 
0216     ireq->gpio = cpu_to_le16(gpio);
0217     sg_init_one(&req_sg, ireq, sizeof(*ireq));
0218     sg_init_one(&res_sg, ires, sizeof(*ires));
0219     sgs[0] = &req_sg;
0220     sgs[1] = &res_sg;
0221 
0222     ret = virtqueue_add_sgs(vgpio->event_vq, sgs, 1, 1, irq_line, GFP_ATOMIC);
0223     if (ret) {
0224         dev_err(&vgpio->vdev->dev, "failed to add request to eventq\n");
0225         return;
0226     }
0227 
0228     irq_line->queued = true;
0229     virtqueue_kick(vgpio->event_vq);
0230 }
0231 
0232 static void virtio_gpio_irq_enable(struct irq_data *d)
0233 {
0234     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0235     struct virtio_gpio *vgpio = gpiochip_get_data(gc);
0236     struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
0237 
0238     raw_spin_lock(&vgpio->eventq_lock);
0239     irq_line->disabled = false;
0240     irq_line->masked = false;
0241     irq_line->queue_pending = true;
0242     raw_spin_unlock(&vgpio->eventq_lock);
0243 
0244     irq_line->update_pending = true;
0245 }
0246 
0247 static void virtio_gpio_irq_disable(struct irq_data *d)
0248 {
0249     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0250     struct virtio_gpio *vgpio = gpiochip_get_data(gc);
0251     struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
0252 
0253     raw_spin_lock(&vgpio->eventq_lock);
0254     irq_line->disabled = true;
0255     irq_line->masked = true;
0256     irq_line->queue_pending = false;
0257     raw_spin_unlock(&vgpio->eventq_lock);
0258 
0259     irq_line->update_pending = true;
0260 }
0261 
0262 static void virtio_gpio_irq_mask(struct irq_data *d)
0263 {
0264     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0265     struct virtio_gpio *vgpio = gpiochip_get_data(gc);
0266     struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
0267 
0268     raw_spin_lock(&vgpio->eventq_lock);
0269     irq_line->masked = true;
0270     raw_spin_unlock(&vgpio->eventq_lock);
0271 }
0272 
0273 static void virtio_gpio_irq_unmask(struct irq_data *d)
0274 {
0275     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0276     struct virtio_gpio *vgpio = gpiochip_get_data(gc);
0277     struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
0278 
0279     raw_spin_lock(&vgpio->eventq_lock);
0280     irq_line->masked = false;
0281 
0282     /* Queue the buffer unconditionally on unmask */
0283     virtio_gpio_irq_prepare(vgpio, d->hwirq);
0284     raw_spin_unlock(&vgpio->eventq_lock);
0285 }
0286 
0287 static int virtio_gpio_irq_set_type(struct irq_data *d, unsigned int type)
0288 {
0289     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0290     struct virtio_gpio *vgpio = gpiochip_get_data(gc);
0291     struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
0292 
0293     switch (type) {
0294     case IRQ_TYPE_EDGE_RISING:
0295         type = VIRTIO_GPIO_IRQ_TYPE_EDGE_RISING;
0296         break;
0297     case IRQ_TYPE_EDGE_FALLING:
0298         type = VIRTIO_GPIO_IRQ_TYPE_EDGE_FALLING;
0299         break;
0300     case IRQ_TYPE_EDGE_BOTH:
0301         type = VIRTIO_GPIO_IRQ_TYPE_EDGE_BOTH;
0302         break;
0303     case IRQ_TYPE_LEVEL_LOW:
0304         type = VIRTIO_GPIO_IRQ_TYPE_LEVEL_LOW;
0305         break;
0306     case IRQ_TYPE_LEVEL_HIGH:
0307         type = VIRTIO_GPIO_IRQ_TYPE_LEVEL_HIGH;
0308         break;
0309     default:
0310         dev_err(&vgpio->vdev->dev, "unsupported irq type: %u\n", type);
0311         return -EINVAL;
0312     }
0313 
0314     irq_line->type = type;
0315     irq_line->update_pending = true;
0316 
0317     return 0;
0318 }
0319 
0320 static void virtio_gpio_irq_bus_lock(struct irq_data *d)
0321 {
0322     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0323     struct virtio_gpio *vgpio = gpiochip_get_data(gc);
0324 
0325     mutex_lock(&vgpio->irq_lock);
0326 }
0327 
0328 static void virtio_gpio_irq_bus_sync_unlock(struct irq_data *d)
0329 {
0330     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0331     struct virtio_gpio *vgpio = gpiochip_get_data(gc);
0332     struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
0333     u8 type = irq_line->disabled ? VIRTIO_GPIO_IRQ_TYPE_NONE : irq_line->type;
0334     unsigned long flags;
0335 
0336     if (irq_line->update_pending) {
0337         irq_line->update_pending = false;
0338         virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_IRQ_TYPE, d->hwirq, type,
0339                 NULL);
0340 
0341         /* Queue the buffer only after interrupt is enabled */
0342         raw_spin_lock_irqsave(&vgpio->eventq_lock, flags);
0343         if (irq_line->queue_pending) {
0344             irq_line->queue_pending = false;
0345             virtio_gpio_irq_prepare(vgpio, d->hwirq);
0346         }
0347         raw_spin_unlock_irqrestore(&vgpio->eventq_lock, flags);
0348     }
0349 
0350     mutex_unlock(&vgpio->irq_lock);
0351 }
0352 
0353 static struct irq_chip vgpio_irq_chip = {
0354     .name           = "virtio-gpio",
0355     .irq_enable     = virtio_gpio_irq_enable,
0356     .irq_disable        = virtio_gpio_irq_disable,
0357     .irq_mask       = virtio_gpio_irq_mask,
0358     .irq_unmask     = virtio_gpio_irq_unmask,
0359     .irq_set_type       = virtio_gpio_irq_set_type,
0360 
0361     /* These are required to implement irqchip for slow busses */
0362     .irq_bus_lock       = virtio_gpio_irq_bus_lock,
0363     .irq_bus_sync_unlock    = virtio_gpio_irq_bus_sync_unlock,
0364 };
0365 
0366 static bool ignore_irq(struct virtio_gpio *vgpio, int gpio,
0367                struct vgpio_irq_line *irq_line)
0368 {
0369     bool ignore = false;
0370 
0371     raw_spin_lock(&vgpio->eventq_lock);
0372     irq_line->queued = false;
0373 
0374     /* Interrupt is disabled currently */
0375     if (irq_line->masked || irq_line->disabled) {
0376         ignore = true;
0377         goto unlock;
0378     }
0379 
0380     /*
0381      * Buffer is returned as the interrupt was disabled earlier, but is
0382      * enabled again now. Requeue the buffers.
0383      */
0384     if (irq_line->ires.status == VIRTIO_GPIO_IRQ_STATUS_INVALID) {
0385         virtio_gpio_irq_prepare(vgpio, gpio);
0386         ignore = true;
0387         goto unlock;
0388     }
0389 
0390     if (WARN_ON(irq_line->ires.status != VIRTIO_GPIO_IRQ_STATUS_VALID))
0391         ignore = true;
0392 
0393 unlock:
0394     raw_spin_unlock(&vgpio->eventq_lock);
0395 
0396     return ignore;
0397 }
0398 
0399 static void virtio_gpio_event_vq(struct virtqueue *vq)
0400 {
0401     struct virtio_gpio *vgpio = vq->vdev->priv;
0402     struct device *dev = &vgpio->vdev->dev;
0403     struct vgpio_irq_line *irq_line;
0404     int gpio, ret;
0405     unsigned int len;
0406 
0407     while (true) {
0408         irq_line = virtqueue_get_buf(vgpio->event_vq, &len);
0409         if (!irq_line)
0410             break;
0411 
0412         if (len != sizeof(irq_line->ires)) {
0413             dev_err(dev, "irq with incorrect length (%u : %u)\n",
0414                 len, (unsigned int)sizeof(irq_line->ires));
0415             continue;
0416         }
0417 
0418         /*
0419          * Find GPIO line number from the offset of irq_line within the
0420          * irq_lines block. We can also get GPIO number from
0421          * irq-request, but better not to rely on a buffer returned by
0422          * remote.
0423          */
0424         gpio = irq_line - vgpio->irq_lines;
0425         WARN_ON(gpio >= vgpio->gc.ngpio);
0426 
0427         if (unlikely(ignore_irq(vgpio, gpio, irq_line)))
0428             continue;
0429 
0430         ret = generic_handle_domain_irq(vgpio->gc.irq.domain, gpio);
0431         if (ret)
0432             dev_err(dev, "failed to handle interrupt: %d\n", ret);
0433     }
0434 }
0435 
0436 static void virtio_gpio_request_vq(struct virtqueue *vq)
0437 {
0438     struct virtio_gpio_line *line;
0439     unsigned int len;
0440 
0441     do {
0442         line = virtqueue_get_buf(vq, &len);
0443         if (!line)
0444             return;
0445 
0446         line->rxlen = len;
0447         complete(&line->completion);
0448     } while (1);
0449 }
0450 
0451 static void virtio_gpio_free_vqs(struct virtio_device *vdev)
0452 {
0453     virtio_reset_device(vdev);
0454     vdev->config->del_vqs(vdev);
0455 }
0456 
0457 static int virtio_gpio_alloc_vqs(struct virtio_gpio *vgpio,
0458                  struct virtio_device *vdev)
0459 {
0460     const char * const names[] = { "requestq", "eventq" };
0461     vq_callback_t *cbs[] = {
0462         virtio_gpio_request_vq,
0463         virtio_gpio_event_vq,
0464     };
0465     struct virtqueue *vqs[2] = { NULL, NULL };
0466     int ret;
0467 
0468     ret = virtio_find_vqs(vdev, vgpio->irq_lines ? 2 : 1, vqs, cbs, names, NULL);
0469     if (ret) {
0470         dev_err(&vdev->dev, "failed to find vqs: %d\n", ret);
0471         return ret;
0472     }
0473 
0474     if (!vqs[0]) {
0475         dev_err(&vdev->dev, "failed to find requestq vq\n");
0476         goto out;
0477     }
0478     vgpio->request_vq = vqs[0];
0479 
0480     if (vgpio->irq_lines && !vqs[1]) {
0481         dev_err(&vdev->dev, "failed to find eventq vq\n");
0482         goto out;
0483     }
0484     vgpio->event_vq = vqs[1];
0485 
0486     return 0;
0487 
0488 out:
0489     if (vqs[0] || vqs[1])
0490         virtio_gpio_free_vqs(vdev);
0491 
0492     return -ENODEV;
0493 }
0494 
0495 static const char **virtio_gpio_get_names(struct virtio_gpio *vgpio,
0496                       u32 gpio_names_size, u16 ngpio)
0497 {
0498     struct virtio_gpio_response_get_names *res;
0499     struct device *dev = &vgpio->vdev->dev;
0500     u8 *gpio_names, *str;
0501     const char **names;
0502     int i, ret, len;
0503 
0504     if (!gpio_names_size)
0505         return NULL;
0506 
0507     len = sizeof(*res) + gpio_names_size;
0508     res = devm_kzalloc(dev, len, GFP_KERNEL);
0509     if (!res)
0510         return NULL;
0511     gpio_names = res->value;
0512 
0513     ret = _virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_GET_NAMES, 0, 0, NULL,
0514                    res, len);
0515     if (ret) {
0516         dev_err(dev, "Failed to get GPIO names: %d\n", ret);
0517         return NULL;
0518     }
0519 
0520     names = devm_kcalloc(dev, ngpio, sizeof(*names), GFP_KERNEL);
0521     if (!names)
0522         return NULL;
0523 
0524     /* NULL terminate the string instead of checking it */
0525     gpio_names[gpio_names_size - 1] = '\0';
0526 
0527     for (i = 0, str = gpio_names; i < ngpio; i++) {
0528         names[i] = str;
0529         str += strlen(str) + 1; /* zero-length strings are allowed */
0530 
0531         if (str > gpio_names + gpio_names_size) {
0532             dev_err(dev, "gpio_names block is too short (%d)\n", i);
0533             return NULL;
0534         }
0535     }
0536 
0537     return names;
0538 }
0539 
0540 static int virtio_gpio_probe(struct virtio_device *vdev)
0541 {
0542     struct virtio_gpio_config config;
0543     struct device *dev = &vdev->dev;
0544     struct virtio_gpio *vgpio;
0545     u32 gpio_names_size;
0546     u16 ngpio;
0547     int ret, i;
0548 
0549     vgpio = devm_kzalloc(dev, sizeof(*vgpio), GFP_KERNEL);
0550     if (!vgpio)
0551         return -ENOMEM;
0552 
0553     /* Read configuration */
0554     virtio_cread_bytes(vdev, 0, &config, sizeof(config));
0555     gpio_names_size = le32_to_cpu(config.gpio_names_size);
0556     ngpio = le16_to_cpu(config.ngpio);
0557     if (!ngpio) {
0558         dev_err(dev, "Number of GPIOs can't be zero\n");
0559         return -EINVAL;
0560     }
0561 
0562     vgpio->lines = devm_kcalloc(dev, ngpio, sizeof(*vgpio->lines), GFP_KERNEL);
0563     if (!vgpio->lines)
0564         return -ENOMEM;
0565 
0566     for (i = 0; i < ngpio; i++) {
0567         mutex_init(&vgpio->lines[i].lock);
0568         init_completion(&vgpio->lines[i].completion);
0569     }
0570 
0571     mutex_init(&vgpio->lock);
0572     vdev->priv = vgpio;
0573 
0574     vgpio->vdev         = vdev;
0575     vgpio->gc.free          = virtio_gpio_free;
0576     vgpio->gc.get_direction     = virtio_gpio_get_direction;
0577     vgpio->gc.direction_input   = virtio_gpio_direction_input;
0578     vgpio->gc.direction_output  = virtio_gpio_direction_output;
0579     vgpio->gc.get           = virtio_gpio_get;
0580     vgpio->gc.set           = virtio_gpio_set;
0581     vgpio->gc.ngpio         = ngpio;
0582     vgpio->gc.base          = -1; /* Allocate base dynamically */
0583     vgpio->gc.label         = dev_name(dev);
0584     vgpio->gc.parent        = dev;
0585     vgpio->gc.owner         = THIS_MODULE;
0586     vgpio->gc.can_sleep     = true;
0587 
0588     /* Interrupt support */
0589     if (virtio_has_feature(vdev, VIRTIO_GPIO_F_IRQ)) {
0590         vgpio->irq_lines = devm_kcalloc(dev, ngpio, sizeof(*vgpio->irq_lines), GFP_KERNEL);
0591         if (!vgpio->irq_lines)
0592             return -ENOMEM;
0593 
0594         /* The event comes from the outside so no parent handler */
0595         vgpio->gc.irq.parent_handler    = NULL;
0596         vgpio->gc.irq.num_parents   = 0;
0597         vgpio->gc.irq.parents       = NULL;
0598         vgpio->gc.irq.default_type  = IRQ_TYPE_NONE;
0599         vgpio->gc.irq.handler       = handle_level_irq;
0600         vgpio->gc.irq.chip      = &vgpio_irq_chip;
0601 
0602         for (i = 0; i < ngpio; i++) {
0603             vgpio->irq_lines[i].type = VIRTIO_GPIO_IRQ_TYPE_NONE;
0604             vgpio->irq_lines[i].disabled = true;
0605             vgpio->irq_lines[i].masked = true;
0606         }
0607 
0608         mutex_init(&vgpio->irq_lock);
0609         raw_spin_lock_init(&vgpio->eventq_lock);
0610     }
0611 
0612     ret = virtio_gpio_alloc_vqs(vgpio, vdev);
0613     if (ret)
0614         return ret;
0615 
0616     /* Mark the device ready to perform operations from within probe() */
0617     virtio_device_ready(vdev);
0618 
0619     vgpio->gc.names = virtio_gpio_get_names(vgpio, gpio_names_size, ngpio);
0620 
0621     ret = gpiochip_add_data(&vgpio->gc, vgpio);
0622     if (ret) {
0623         virtio_gpio_free_vqs(vdev);
0624         dev_err(dev, "Failed to add virtio-gpio controller\n");
0625     }
0626 
0627     return ret;
0628 }
0629 
0630 static void virtio_gpio_remove(struct virtio_device *vdev)
0631 {
0632     struct virtio_gpio *vgpio = vdev->priv;
0633 
0634     gpiochip_remove(&vgpio->gc);
0635     virtio_gpio_free_vqs(vdev);
0636 }
0637 
0638 static const struct virtio_device_id id_table[] = {
0639     { VIRTIO_ID_GPIO, VIRTIO_DEV_ANY_ID },
0640     {},
0641 };
0642 MODULE_DEVICE_TABLE(virtio, id_table);
0643 
0644 static const unsigned int features[] = {
0645     VIRTIO_GPIO_F_IRQ,
0646 };
0647 
0648 static struct virtio_driver virtio_gpio_driver = {
0649     .feature_table      = features,
0650     .feature_table_size = ARRAY_SIZE(features),
0651     .id_table       = id_table,
0652     .probe          = virtio_gpio_probe,
0653     .remove         = virtio_gpio_remove,
0654     .driver         = {
0655         .name       = KBUILD_MODNAME,
0656         .owner      = THIS_MODULE,
0657     },
0658 };
0659 module_virtio_driver(virtio_gpio_driver);
0660 
0661 MODULE_AUTHOR("Enrico Weigelt, metux IT consult <info@metux.net>");
0662 MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
0663 MODULE_DESCRIPTION("VirtIO GPIO driver");
0664 MODULE_LICENSE("GPL");