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