0001
0002
0003
0004
0005
0006 #include <linux/moduleparam.h>
0007 #include <linux/virtio_config.h>
0008
0009 #include "virtio_card.h"
0010
0011
0012
0013
0014
0015
0016
0017
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
0029
0030
0031
0032
0033 void virtsnd_ctl_msg_ref(struct virtio_snd_msg *msg)
0034 {
0035 refcount_inc(&msg->ref_count);
0036 }
0037
0038
0039
0040
0041
0042
0043
0044
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
0054
0055
0056
0057
0058 void *virtsnd_ctl_msg_request(struct virtio_snd_msg *msg)
0059 {
0060 return sg_virt(&msg->sg_request);
0061 }
0062
0063
0064
0065
0066
0067
0068
0069 void *virtsnd_ctl_msg_response(struct virtio_snd_msg *msg)
0070 {
0071 return sg_virt(&msg->sg_response);
0072 }
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
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
0104 refcount_set(&msg->ref_count, 1);
0105
0106 return msg;
0107 }
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
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
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
0166
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
0214
0215
0216
0217
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
0229
0230
0231
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
0251
0252
0253
0254
0255
0256
0257
0258
0259
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
0286
0287
0288
0289
0290
0291
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 }