Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * virtio-snd: Virtio sound device
0004  * Copyright (C) 2021 OpenSynergy GmbH
0005  */
0006 #include <linux/moduleparam.h>
0007 #include <linux/virtio_config.h>
0008 
0009 #include "virtio_card.h"
0010 
0011 /**
0012  * struct virtio_snd_msg - Control message.
0013  * @sg_request: Scattergather list containing a device request (header).
0014  * @sg_response: Scattergather list containing a device response (status).
0015  * @list: Pending message list entry.
0016  * @notify: Request completed notification.
0017  * @ref_count: Reference count used to manage a message lifetime.
0018  */
0019 struct virtio_snd_msg {
0020     struct scatterlist sg_request;
0021     struct scatterlist sg_response;
0022     struct list_head list;
0023     struct completion notify;
0024     refcount_t ref_count;
0025 };
0026 
0027 /**
0028  * virtsnd_ctl_msg_ref() - Increment reference counter for the message.
0029  * @msg: Control message.
0030  *
0031  * Context: Any context.
0032  */
0033 void virtsnd_ctl_msg_ref(struct virtio_snd_msg *msg)
0034 {
0035     refcount_inc(&msg->ref_count);
0036 }
0037 
0038 /**
0039  * virtsnd_ctl_msg_unref() - Decrement reference counter for the message.
0040  * @msg: Control message.
0041  *
0042  * The message will be freed when the ref_count value is 0.
0043  *
0044  * Context: Any context.
0045  */
0046 void virtsnd_ctl_msg_unref(struct virtio_snd_msg *msg)
0047 {
0048     if (refcount_dec_and_test(&msg->ref_count))
0049         kfree(msg);
0050 }
0051 
0052 /**
0053  * virtsnd_ctl_msg_request() - Get a pointer to the request header.
0054  * @msg: Control message.
0055  *
0056  * Context: Any context.
0057  */
0058 void *virtsnd_ctl_msg_request(struct virtio_snd_msg *msg)
0059 {
0060     return sg_virt(&msg->sg_request);
0061 }
0062 
0063 /**
0064  * virtsnd_ctl_msg_response() - Get a pointer to the response header.
0065  * @msg: Control message.
0066  *
0067  * Context: Any context.
0068  */
0069 void *virtsnd_ctl_msg_response(struct virtio_snd_msg *msg)
0070 {
0071     return sg_virt(&msg->sg_response);
0072 }
0073 
0074 /**
0075  * virtsnd_ctl_msg_alloc() - Allocate and initialize a control message.
0076  * @request_size: Size of request header.
0077  * @response_size: Size of response header.
0078  * @gfp: Kernel flags for memory allocation.
0079  *
0080  * The message will be automatically freed when the ref_count value is 0.
0081  *
0082  * Context: Any context. May sleep if @gfp flags permit.
0083  * Return: Allocated message on success, NULL on failure.
0084  */
0085 struct virtio_snd_msg *virtsnd_ctl_msg_alloc(size_t request_size,
0086                          size_t response_size, gfp_t gfp)
0087 {
0088     struct virtio_snd_msg *msg;
0089 
0090     if (!request_size || !response_size)
0091         return NULL;
0092 
0093     msg = kzalloc(sizeof(*msg) + request_size + response_size, gfp);
0094     if (!msg)
0095         return NULL;
0096 
0097     sg_init_one(&msg->sg_request, (u8 *)msg + sizeof(*msg), request_size);
0098     sg_init_one(&msg->sg_response, (u8 *)msg + sizeof(*msg) + request_size,
0099             response_size);
0100 
0101     INIT_LIST_HEAD(&msg->list);
0102     init_completion(&msg->notify);
0103     /* This reference is dropped in virtsnd_ctl_msg_complete(). */
0104     refcount_set(&msg->ref_count, 1);
0105 
0106     return msg;
0107 }
0108 
0109 /**
0110  * virtsnd_ctl_msg_send() - Send a control message.
0111  * @snd: VirtIO sound device.
0112  * @msg: Control message.
0113  * @out_sgs: Additional sg-list to attach to the request header (may be NULL).
0114  * @in_sgs: Additional sg-list to attach to the response header (may be NULL).
0115  * @nowait: Flag indicating whether to wait for completion.
0116  *
0117  * Context: Any context. Takes and releases the control queue spinlock.
0118  *          May sleep if @nowait is false.
0119  * Return: 0 on success, -errno on failure.
0120  */
0121 int virtsnd_ctl_msg_send(struct virtio_snd *snd, struct virtio_snd_msg *msg,
0122              struct scatterlist *out_sgs,
0123              struct scatterlist *in_sgs, bool nowait)
0124 {
0125     struct virtio_device *vdev = snd->vdev;
0126     struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
0127     unsigned int js = msecs_to_jiffies(virtsnd_msg_timeout_ms);
0128     struct virtio_snd_hdr *request = virtsnd_ctl_msg_request(msg);
0129     struct virtio_snd_hdr *response = virtsnd_ctl_msg_response(msg);
0130     unsigned int nouts = 0;
0131     unsigned int nins = 0;
0132     struct scatterlist *psgs[4];
0133     bool notify = false;
0134     unsigned long flags;
0135     int rc;
0136 
0137     virtsnd_ctl_msg_ref(msg);
0138 
0139     /* Set the default status in case the message was canceled. */
0140     response->code = cpu_to_le32(VIRTIO_SND_S_IO_ERR);
0141 
0142     psgs[nouts++] = &msg->sg_request;
0143     if (out_sgs)
0144         psgs[nouts++] = out_sgs;
0145 
0146     psgs[nouts + nins++] = &msg->sg_response;
0147     if (in_sgs)
0148         psgs[nouts + nins++] = in_sgs;
0149 
0150     spin_lock_irqsave(&queue->lock, flags);
0151     rc = virtqueue_add_sgs(queue->vqueue, psgs, nouts, nins, msg,
0152                    GFP_ATOMIC);
0153     if (!rc) {
0154         notify = virtqueue_kick_prepare(queue->vqueue);
0155 
0156         list_add_tail(&msg->list, &snd->ctl_msgs);
0157     }
0158     spin_unlock_irqrestore(&queue->lock, flags);
0159 
0160     if (rc) {
0161         dev_err(&vdev->dev, "failed to send control message (0x%08x)\n",
0162             le32_to_cpu(request->code));
0163 
0164         /*
0165          * Since in this case virtsnd_ctl_msg_complete() will not be
0166          * called, it is necessary to decrement the reference count.
0167          */
0168         virtsnd_ctl_msg_unref(msg);
0169 
0170         goto on_exit;
0171     }
0172 
0173     if (notify)
0174         virtqueue_notify(queue->vqueue);
0175 
0176     if (nowait)
0177         goto on_exit;
0178 
0179     rc = wait_for_completion_interruptible_timeout(&msg->notify, js);
0180     if (rc <= 0) {
0181         if (!rc) {
0182             dev_err(&vdev->dev,
0183                 "control message (0x%08x) timeout\n",
0184                 le32_to_cpu(request->code));
0185             rc = -ETIMEDOUT;
0186         }
0187 
0188         goto on_exit;
0189     }
0190 
0191     switch (le32_to_cpu(response->code)) {
0192     case VIRTIO_SND_S_OK:
0193         rc = 0;
0194         break;
0195     case VIRTIO_SND_S_NOT_SUPP:
0196         rc = -EOPNOTSUPP;
0197         break;
0198     case VIRTIO_SND_S_IO_ERR:
0199         rc = -EIO;
0200         break;
0201     default:
0202         rc = -EINVAL;
0203         break;
0204     }
0205 
0206 on_exit:
0207     virtsnd_ctl_msg_unref(msg);
0208 
0209     return rc;
0210 }
0211 
0212 /**
0213  * virtsnd_ctl_msg_complete() - Complete a control message.
0214  * @msg: Control message.
0215  *
0216  * Context: Any context. Expects the control queue spinlock to be held by
0217  *          caller.
0218  */
0219 void virtsnd_ctl_msg_complete(struct virtio_snd_msg *msg)
0220 {
0221     list_del(&msg->list);
0222     complete(&msg->notify);
0223 
0224     virtsnd_ctl_msg_unref(msg);
0225 }
0226 
0227 /**
0228  * virtsnd_ctl_msg_cancel_all() - Cancel all pending control messages.
0229  * @snd: VirtIO sound device.
0230  *
0231  * Context: Any context.
0232  */
0233 void virtsnd_ctl_msg_cancel_all(struct virtio_snd *snd)
0234 {
0235     struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
0236     unsigned long flags;
0237 
0238     spin_lock_irqsave(&queue->lock, flags);
0239     while (!list_empty(&snd->ctl_msgs)) {
0240         struct virtio_snd_msg *msg =
0241             list_first_entry(&snd->ctl_msgs, struct virtio_snd_msg,
0242                      list);
0243 
0244         virtsnd_ctl_msg_complete(msg);
0245     }
0246     spin_unlock_irqrestore(&queue->lock, flags);
0247 }
0248 
0249 /**
0250  * virtsnd_ctl_query_info() - Query the item configuration from the device.
0251  * @snd: VirtIO sound device.
0252  * @command: Control request code (VIRTIO_SND_R_XXX_INFO).
0253  * @start_id: Item start identifier.
0254  * @count: Item count to query.
0255  * @size: Item information size in bytes.
0256  * @info: Buffer for storing item information.
0257  *
0258  * Context: Any context that permits to sleep.
0259  * Return: 0 on success, -errno on failure.
0260  */
0261 int virtsnd_ctl_query_info(struct virtio_snd *snd, int command, int start_id,
0262                int count, size_t size, void *info)
0263 {
0264     struct virtio_snd_msg *msg;
0265     struct virtio_snd_query_info *query;
0266     struct scatterlist sg;
0267 
0268     msg = virtsnd_ctl_msg_alloc(sizeof(*query),
0269                     sizeof(struct virtio_snd_hdr), GFP_KERNEL);
0270     if (!msg)
0271         return -ENOMEM;
0272 
0273     query = virtsnd_ctl_msg_request(msg);
0274     query->hdr.code = cpu_to_le32(command);
0275     query->start_id = cpu_to_le32(start_id);
0276     query->count = cpu_to_le32(count);
0277     query->size = cpu_to_le32(size);
0278 
0279     sg_init_one(&sg, info, count * size);
0280 
0281     return virtsnd_ctl_msg_send(snd, msg, NULL, &sg, false);
0282 }
0283 
0284 /**
0285  * virtsnd_ctl_notify_cb() - Process all completed control messages.
0286  * @vqueue: Underlying control virtqueue.
0287  *
0288  * This callback function is called upon a vring interrupt request from the
0289  * device.
0290  *
0291  * Context: Interrupt context. Takes and releases the control queue spinlock.
0292  */
0293 void virtsnd_ctl_notify_cb(struct virtqueue *vqueue)
0294 {
0295     struct virtio_snd *snd = vqueue->vdev->priv;
0296     struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
0297     struct virtio_snd_msg *msg;
0298     u32 length;
0299     unsigned long flags;
0300 
0301     spin_lock_irqsave(&queue->lock, flags);
0302     do {
0303         virtqueue_disable_cb(vqueue);
0304         while ((msg = virtqueue_get_buf(vqueue, &length)))
0305             virtsnd_ctl_msg_complete(msg);
0306         if (unlikely(virtqueue_is_broken(vqueue)))
0307             break;
0308     } while (!virtqueue_enable_cb(vqueue));
0309     spin_unlock_irqrestore(&queue->lock, flags);
0310 }