| 1 | |
| 2 | |
| 3 | * virtio-snd: Virtio sound device
|
| 4 | * Copyright (C) 2021 OpenSynergy GmbH
|
| 5 |
|
| 6 | #include <linux/moduleparam.h>
|
| 7 | #include <linux/virtio_config.h>
|
| 8 |
|
| 9 | #include "virtio_card.h"
|
| 10 |
|
| 11 | |
| 12 | * struct virtio_snd_msg - Control message.
|
| 13 | * @sg_request: Scattergather list containing a device request (header).
|
| 14 | * @sg_response: Scattergather list containing a device response (status).
|
| 15 | * @list: Pending message list entry.
|
| 16 | * @notify: Request completed notification.
|
| 17 | * @ref_count: Reference count used to manage a message lifetime.
|
| 18 |
|
| 19 | struct virtio_snd_msg {
|
| 20 | struct scatterlist sg_request;
|
| 21 | struct scatterlist sg_response;
|
| 22 | struct list_head list;
|
| 23 | struct completion notify;
|
| 24 | refcount_t ref_count;
|
| 25 | };
|
| 26 |
|
| 27 | |
| 28 | * virtsnd_ctl_msg_ref() - Increment reference counter for the message.
|
| 29 | * @msg: Control message.
|
| 30 | *
|
| 31 | * Context: Any context.
|
| 32 |
|
| 33 | void virtsnd_ctl_msg_ref(struct virtio_snd_msg *msg)
|
| 34 | {
|
| 35 | refcount_inc(&msg->ref_count);
|
| 36 | }
|
| 37 |
|
| 38 | |
| 39 | * virtsnd_ctl_msg_unref() - Decrement reference counter for the message.
|
| 40 | * @msg: Control message.
|
| 41 | *
|
| 42 | * The message will be freed when the ref_count value is 0.
|
| 43 | *
|
| 44 | * Context: Any context.
|
| 45 |
|
| 46 | void virtsnd_ctl_msg_unref(struct virtio_snd_msg *msg)
|
| 47 | {
|
| 48 | if (refcount_dec_and_test(&msg->ref_count))
|
| 49 | kfree(msg);
|
| 50 | }
|
| 51 |
|
| 52 | |
| 53 | * virtsnd_ctl_msg_request() - Get a pointer to the request header.
|
| 54 | * @msg: Control message.
|
| 55 | *
|
| 56 | * Context: Any context.
|
| 57 |
|
| 58 | void *virtsnd_ctl_msg_request(struct virtio_snd_msg *msg)
|
| 59 | {
|
| 60 | return sg_virt(&msg->sg_request);
|
| 61 | }
|
| 62 |
|
| 63 | |
| 64 | * virtsnd_ctl_msg_response() - Get a pointer to the response header.
|
| 65 | * @msg: Control message.
|
| 66 | *
|
| 67 | * Context: Any context.
|
| 68 |
|
| 69 | void *virtsnd_ctl_msg_response(struct virtio_snd_msg *msg)
|
| 70 | {
|
| 71 | return sg_virt(&msg->sg_response);
|
| 72 | }
|
| 73 |
|
| 74 | |
| 75 | * virtsnd_ctl_msg_alloc() - Allocate and initialize a control message.
|
| 76 | * @request_size: Size of request header.
|
| 77 | * @response_size: Size of response header.
|
| 78 | * @gfp: Kernel flags for memory allocation.
|
| 79 | *
|
| 80 | * The message will be automatically freed when the ref_count value is 0.
|
| 81 | *
|
| 82 | * Context: Any context. May sleep if @gfp flags permit.
|
| 83 | * Return: Allocated message on success, NULL on failure.
|
| 84 |
|
| 85 | struct virtio_snd_msg *virtsnd_ctl_msg_alloc(size_t request_size,
|
| 86 | size_t response_size, gfp_t gfp)
|
| 87 | {
|
| 88 | struct virtio_snd_msg *msg;
|
| 89 |
|
| 90 | if (!request_size || !response_size)
|
| 91 | return NULL;
|
| 92 |
|
| 93 | msg = kzalloc(sizeof(*msg) + request_size + response_size, gfp);
|
| 94 | if (!msg)
|
| 95 | return NULL;
|
| 96 |
|
| 97 | sg_init_one(&msg->sg_request, (u8 *)msg + sizeof(*msg), request_size);
|
| 98 | sg_init_one(&msg->sg_response, (u8 *)msg + sizeof(*msg) + request_size,
|
| 99 | response_size);
|
| 100 |
|
| 101 | INIT_LIST_HEAD(&msg->list);
|
| 102 | init_completion(&msg->notify);
|
| 103 |
|
| 104 | refcount_set(&msg->ref_count, 1);
|
| 105 |
|
| 106 | return msg;
|
| 107 | }
|
| 108 |
|
| 109 | |
| 110 | * virtsnd_ctl_msg_send() - Send a control message.
|
| 111 | * @snd: VirtIO sound device.
|
| 112 | * @msg: Control message.
|
| 113 | * @out_sgs: Additional sg-list to attach to the request header (may be NULL).
|
| 114 | * @in_sgs: Additional sg-list to attach to the response header (may be NULL).
|
| 115 | * @nowait: Flag indicating whether to wait for completion.
|
| 116 | *
|
| 117 | * Context: Any context. Takes and releases the control queue spinlock.
|
| 118 | * May sleep if @nowait is false.
|
| 119 | * Return: 0 on success, -errno on failure.
|
| 120 |
|
| 121 | int virtsnd_ctl_msg_send(struct virtio_snd *snd, struct virtio_snd_msg *msg,
|
| 122 | struct scatterlist *out_sgs,
|
| 123 | struct scatterlist *in_sgs, bool nowait)
|
| 124 | {
|
| 125 | struct virtio_device *vdev = snd->vdev;
|
| 126 | struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
|
| 127 | unsigned int js = msecs_to_jiffies(virtsnd_msg_timeout_ms);
|
| 128 | struct virtio_snd_hdr *request = virtsnd_ctl_msg_request(msg);
|
| 129 | struct virtio_snd_hdr *response = virtsnd_ctl_msg_response(msg);
|
| 130 | unsigned int nouts = 0;
|
| 131 | unsigned int nins = 0;
|
| 132 | struct scatterlist *psgs[4];
|
| 133 | bool notify = false;
|
| 134 | int rc;
|
| 135 |
|
| 136 | virtsnd_ctl_msg_ref(msg);
|
| 137 |
|
| 138 |
|
| 139 | response->code = cpu_to_le32(VIRTIO_SND_S_IO_ERR);
|
| 140 |
|
| 141 | psgs[nouts++] = &msg->sg_request;
|
| 142 | if (out_sgs)
|
| 143 | psgs[nouts++] = out_sgs;
|
| 144 |
|
| 145 | psgs[nouts + nins++] = &msg->sg_response;
|
| 146 | if (in_sgs)
|
| 147 | psgs[nouts + nins++] = in_sgs;
|
| 148 |
|
| 149 | scoped_guard(spinlock_irqsave, &queue->lock) {
|
| 150 | rc = virtqueue_add_sgs(queue->vqueue, psgs, nouts, nins, msg,
|
| 151 | GFP_ATOMIC);
|
| 152 | if (!rc) {
|
| 153 | notify = virtqueue_kick_prepare(queue->vqueue);
|
| 154 |
|
| 155 | list_add_tail(&msg->list, &snd->ctl_msgs);
|
| 156 | }
|
| 157 | }
|
| 158 |
|
| 159 | if (rc) {
|
| 160 | dev_err(&vdev->dev, "failed to send control message (0x%08x)\n",
|
| 161 | le32_to_cpu(request->code));
|
| 162 |
|
| 163 | |
| 164 | * Since in this case virtsnd_ctl_msg_complete() will not be
|
| 165 | * called, it is necessary to decrement the reference count.
|
| 166 |
|
| 167 | virtsnd_ctl_msg_unref(msg);
|
| 168 |
|
| 169 | goto on_exit;
|
| 170 | }
|
| 171 |
|
| 172 | if (notify)
|
| 173 | virtqueue_notify(queue->vqueue);
|
| 174 |
|
| 175 | if (nowait)
|
| 176 | goto on_exit;
|
| 177 |
|
| 178 | rc = wait_for_completion_interruptible_timeout(&msg->notify, js);
|
| 179 | if (rc <= 0) {
|
| 180 | if (!rc) {
|
| 181 | dev_err(&vdev->dev,
|
| 182 | "control message (0x%08x) timeout\n",
|
| 183 | le32_to_cpu(request->code));
|
| 184 | rc = -ETIMEDOUT;
|
| 185 | }
|
| 186 |
|
| 187 | goto on_exit;
|
| 188 | }
|
| 189 |
|
| 190 | switch (le32_to_cpu(response->code)) {
|
| 191 | case VIRTIO_SND_S_OK:
|
| 192 | rc = 0;
|
| 193 | break;
|
| 194 | case VIRTIO_SND_S_NOT_SUPP:
|
| 195 | rc = -EOPNOTSUPP;
|
| 196 | break;
|
| 197 | case VIRTIO_SND_S_IO_ERR:
|
| 198 | rc = -EIO;
|
| 199 | break;
|
| 200 | default:
|
| 201 | rc = -EINVAL;
|
| 202 | break;
|
| 203 | }
|
| 204 |
|
| 205 | on_exit:
|
| 206 | virtsnd_ctl_msg_unref(msg);
|
| 207 |
|
| 208 | return rc;
|
| 209 | }
|
| 210 |
|
| 211 | |
| 212 | * virtsnd_ctl_msg_complete() - Complete a control message.
|
| 213 | * @msg: Control message.
|
| 214 | *
|
| 215 | * Context: Any context. Expects the control queue spinlock to be held by
|
| 216 | * caller.
|
| 217 |
|
| 218 | void virtsnd_ctl_msg_complete(struct virtio_snd_msg *msg)
|
| 219 | {
|
| 220 | list_del(&msg->list);
|
| 221 | complete(&msg->notify);
|
| 222 |
|
| 223 | virtsnd_ctl_msg_unref(msg);
|
| 224 | }
|
| 225 |
|
| 226 | |
| 227 | * virtsnd_ctl_msg_cancel_all() - Cancel all pending control messages.
|
| 228 | * @snd: VirtIO sound device.
|
| 229 | *
|
| 230 | * Context: Any context.
|
| 231 |
|
| 232 | void virtsnd_ctl_msg_cancel_all(struct virtio_snd *snd)
|
| 233 | {
|
| 234 | struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
|
| 235 |
|
| 236 | guard(spinlock_irqsave)(&queue->lock);
|
| 237 | while (!list_empty(&snd->ctl_msgs)) {
|
| 238 | struct virtio_snd_msg *msg =
|
| 239 | list_first_entry(&snd->ctl_msgs, struct virtio_snd_msg,
|
| 240 | list);
|
| 241 |
|
| 242 | virtsnd_ctl_msg_complete(msg);
|
| 243 | }
|
| 244 | }
|
| 245 |
|
| 246 | |
| 247 | * virtsnd_ctl_query_info() - Query the item configuration from the device.
|
| 248 | * @snd: VirtIO sound device.
|
| 249 | * @command: Control request code (VIRTIO_SND_R_XXX_INFO).
|
| 250 | * @start_id: Item start identifier.
|
| 251 | * @count: Item count to query.
|
| 252 | * @size: Item information size in bytes.
|
| 253 | * @info: Buffer for storing item information.
|
| 254 | *
|
| 255 | * Context: Any context that permits to sleep.
|
| 256 | * Return: 0 on success, -errno on failure.
|
| 257 |
|
| 258 | int virtsnd_ctl_query_info(struct virtio_snd *snd, int command, int start_id,
|
| 259 | int count, size_t size, void *info)
|
| 260 | {
|
| 261 | struct virtio_snd_msg *msg;
|
| 262 | struct virtio_snd_query_info *query;
|
| 263 | struct scatterlist sg;
|
| 264 |
|
| 265 | msg = virtsnd_ctl_msg_alloc(sizeof(*query),
|
| 266 | sizeof(struct virtio_snd_hdr), GFP_KERNEL);
|
| 267 | if (!msg)
|
| 268 | return -ENOMEM;
|
| 269 |
|
| 270 | query = virtsnd_ctl_msg_request(msg);
|
| 271 | query->hdr.code = cpu_to_le32(command);
|
| 272 | query->start_id = cpu_to_le32(start_id);
|
| 273 | query->count = cpu_to_le32(count);
|
| 274 | query->size = cpu_to_le32(size);
|
| 275 |
|
| 276 | sg_init_one(&sg, info, count * size);
|
| 277 |
|
| 278 | return virtsnd_ctl_msg_send(snd, msg, NULL, &sg, false);
|
| 279 | }
|
| 280 |
|
| 281 | |
| 282 | * virtsnd_ctl_notify_cb() - Process all completed control messages.
|
| 283 | * @vqueue: Underlying control virtqueue.
|
| 284 | *
|
| 285 | * This callback function is called upon a vring interrupt request from the
|
| 286 | * device.
|
| 287 | *
|
| 288 | * Context: Interrupt context. Takes and releases the control queue spinlock.
|
| 289 |
|
| 290 | void virtsnd_ctl_notify_cb(struct virtqueue *vqueue)
|
| 291 | {
|
| 292 | struct virtio_snd *snd = vqueue->vdev->priv;
|
| 293 | struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
|
| 294 | struct virtio_snd_msg *msg;
|
| 295 | u32 length;
|
| 296 |
|
| 297 | guard(spinlock_irqsave)(&queue->lock);
|
| 298 | do {
|
| 299 | virtqueue_disable_cb(vqueue);
|
| 300 | while ((msg = virtqueue_get_buf(vqueue, &length)))
|
| 301 | virtsnd_ctl_msg_complete(msg);
|
| 302 | } while (!virtqueue_enable_cb(vqueue));
|
| 303 | }
|