forked from oasis-tcs/virtio-spec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtio-sound.tex
1179 lines (939 loc) · 41.7 KB
/
virtio-sound.tex
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\section{Sound Device}\label{sec:Device Types / Sound Device}
The virtio sound card is a virtual audio device supporting input and output PCM
streams.
A device is managed by control requests and can send various notifications
through dedicated queues. A driver can transmit PCM frames using message-based
transport or shared memory.
A small part of the specification reuses existing layouts and values from the
High Definition Audio specification (\hyperref[intro:HDA]{HDA}). It allows to
provide the same functionality and assist in two possible cases:
\begin{enumerate}
\item implementation of a universal sound driver,
\item implementation of a sound driver as part of the High Definition Audio
subsystem.
\end{enumerate}
\subsection{Device ID}\label{sec:Device Types / Sound Device / Device ID}
25
\subsection{Virtqueues}\label{sec:Device Types / Sound Device / Virtqueues}
\begin{description}
\item[0] controlq
\item[1] eventq
\item[2] txq
\item[3] rxq
\end{description}
The control queue is used for sending control messages from the driver to
the device.
The event queue is used for sending notifications from the device to the driver.
The tx queue is used to send PCM frames for output streams.
The rx queue is used to receive PCM frames from input streams.
\subsection{Feature Bits}\label{sec:Device Types / Sound Device / Feature Bits}
\begin{description}
\item[VIRTIO_SND_F_CTLS (0)]
Device supports control elements.
\end{description}
\subsection{Device Configuration Layout}\label{sec:Device Types / Sound Device / Device Configuration Layout}
\begin{lstlisting}
struct virtio_snd_config {
le32 jacks;
le32 streams;
le32 chmaps;
le32 controls;
};
\end{lstlisting}
A configuration space contains the following fields:
\begin{description}
\item[\field{jacks}] (driver-read-only) indicates a total number of all available
jacks.
\item[\field{streams}] (driver-read-only) indicates a total number of all available
PCM streams.
\item[\field{chmaps}] (driver-read-only) indicates a total number of all available
channel maps.
\item[\field{controls}] (driver-read-only) indicates a total number of all available
control elements if VIRTIO_SND_F_CTLS has been negotiated.
\end{description}
\subsection{Device Initialization}
\begin{enumerate}
\item Configure the control, event, tx and rx queues.
\item Read the \field{jacks} field and send a control request to query information
about the available jacks.
\item Read the \field{streams} field and send a control request to query information
about the available PCM streams.
\item Read the \field{chmaps} field and send a control request to query information
about the available channel maps.
\item If VIRTIO_SND_F_CTLS has been negotiated, read the \field{controls} field
and send a control request to query information about the available control elements.
\item Populate the event queue with empty buffers.
\end{enumerate}
\drivernormative{\subsubsection}{Device Initialization}{Device Types / Sound Device / Device Initialization}
\begin{itemize}
\item The driver MUST NOT read the \field{controls} field
if VIRTIO_SND_F_CTLS has not been negotiated.
\item The driver MUST populate the event queue with empty buffers of at least
the struct virtio_snd_event size.
\item The driver MUST NOT put a device-readable buffers in the event queue.
\end{itemize}
\subsection{Device Operation}\label{sec:Device Types / Sound Device / Device Operation}
All control messages are placed into the control queue and all notifications
are placed into the event queue. They use the following layout structure and
definitions:
\begin{lstlisting}
enum {
/* jack control request types */
VIRTIO_SND_R_JACK_INFO = 1,
VIRTIO_SND_R_JACK_REMAP,
/* PCM control request types */
VIRTIO_SND_R_PCM_INFO = 0x0100,
VIRTIO_SND_R_PCM_SET_PARAMS,
VIRTIO_SND_R_PCM_PREPARE,
VIRTIO_SND_R_PCM_RELEASE,
VIRTIO_SND_R_PCM_START,
VIRTIO_SND_R_PCM_STOP,
/* channel map control request types */
VIRTIO_SND_R_CHMAP_INFO = 0x0200,
/* control element request types */
VIRTIO_SND_R_CTL_INFO = 0x0300,
VIRTIO_SND_R_CTL_ENUM_ITEMS,
VIRTIO_SND_R_CTL_READ,
VIRTIO_SND_R_CTL_WRITE,
VIRTIO_SND_R_CTL_TLV_READ,
VIRTIO_SND_R_CTL_TLV_WRITE,
VIRTIO_SND_R_CTL_TLV_COMMAND,
/* jack event types */
VIRTIO_SND_EVT_JACK_CONNECTED = 0x1000,
VIRTIO_SND_EVT_JACK_DISCONNECTED,
/* PCM event types */
VIRTIO_SND_EVT_PCM_PERIOD_ELAPSED = 0x1100,
VIRTIO_SND_EVT_PCM_XRUN,
/* control element event types */
VIRTIO_SND_EVT_CTL_NOTIFY = 0x1200,
/* common status codes */
VIRTIO_SND_S_OK = 0x8000,
VIRTIO_SND_S_BAD_MSG,
VIRTIO_SND_S_NOT_SUPP,
VIRTIO_SND_S_IO_ERR
};
/* a common header */
struct virtio_snd_hdr {
le32 code;
};
/* an event notification */
struct virtio_snd_event {
struct virtio_snd_hdr hdr;
le32 data;
};
\end{lstlisting}
A generic control message consists of a request part and a response part.
A request part has, or consists of, a common header containing the following
device-readable field:
\begin{description}
\item[\field{code}] specifies a device request type (VIRTIO_SND_R_*).
\end{description}
A response part has, or consists of, a common header containing the following
device-writable field:
\begin{description}
\item[\field{code}] indicates a device request status (VIRTIO_SND_S_*).
\end{description}
The status field can take one of the following values:
\begin{itemize}
\item VIRTIO_SND_S_OK - success.
\item VIRTIO_SND_S_BAD_MSG - a control message is malformed or contains invalid
parameters.
\item VIRTIO_SND_S_NOT_SUPP - requested operation or parameters are not supported.
\item VIRTIO_SND_S_IO_ERR - an I/O error occurred.
\end{itemize}
The request part may be followed by an additional device-readable payload,
and the response part may be followed by an additional device-writable payload.
An event notification contains the following device-writable fields:
\begin{description}
\item[\field{hdr}] indicates an event type (VIRTIO_SND_EVT_*).
\item[\field{data}] indicates an optional event data.
\end{description}
For all entities involved in the exchange of audio data, the device uses one of
the following data flow directions:
\begin{lstlisting}
enum {
VIRTIO_SND_D_OUTPUT = 0,
VIRTIO_SND_D_INPUT
};
\end{lstlisting}
\subsubsection{Item Information Request}\label{sec:Device Types / Sound Device / Device Operation / Item Information Request}
A special control message is used to request information about any kind of
configuration item. The request part uses the following structure definition:
\begin{lstlisting}
struct virtio_snd_query_info {
struct virtio_snd_hdr hdr;
le32 start_id;
le32 count;
le32 size;
};
\end{lstlisting}
The request contains the following device-readable fields:
\begin{description}
\item[\field{hdr}] specifies a particular item request type (VIRTIO_SND_R_*_INFO).
\item[\field{start_id}] specifies the starting identifier for the item (the range
of available identifiers is limited by the total number of particular items that
is indicated in the device configuration space).
\item[\field{count}] specifies the number of items for which information is
requested (the total number of particular items is indicated in the device
configuration space).
\item[\field{size}] specifies the size of the structure containing information
for one item (used for backward compatibility).
\end{description}
The response consists of the virtio_snd_hdr structure (contains the request
status code), followed by the device-writable information structures of the
item. Each information structure begins with the following common header:
\begin{lstlisting}
struct virtio_snd_info {
le32 hda_fn_nid;
};
\end{lstlisting}
The header contains the following field:
\begin{description}
\item[\field{hda_fn_nid}] indicates a function group node identifier
(see \hyperref[intro:HDA]{HDA}, section 7.1.2). This field can be used to link
together different types of resources (e.g. jacks with streams and channel maps
with streams).
\end{description}
\drivernormative{\subsubsection}{Item Information Request}{Device Types / Sound Device / Item Information Request}
\begin{itemize}
\item The driver MUST NOT set \field{start_id} and \field{count} such that
\field{start_id} + \field{count} is greater than the total number of particular
items that is indicated in the device configuration space.
\item The driver MUST provide a buffer of sizeof(struct virtio_snd_hdr) +
\field{count} * \field{size} bytes for the response.
\end{itemize}
\subsubsection{Relationships with the High Definition Audio Specification}\label{sec:Device Types / Sound Device / Device Operation / Relationships with HDA}
The High Definition Audio specification introduces the codec as part of the
hardware that implements some of the functionality. The codec architecture and
capabilities are described by tree structure of special nodes each of which is
either a function module or a function group (see \hyperref[intro:HDA]{HDA} for
details).
The virtio sound specification assumes that a single codec is implemented in the
device. Function module nodes are simulated by item information structures,
and function group nodes are simulated by the \field{hda_fn_nid} field in each
such structure.
\subsubsection{Jack Control Messages}\label{sec:Device Types / Sound Device / Device Operation / Jack Control Messages}
A jack control request has, or consists of, a common header with the following
layout structure:
\begin{lstlisting}
struct virtio_snd_jack_hdr {
struct virtio_snd_hdr hdr;
le32 jack_id;
};
\end{lstlisting}
The header consists of the following device-readable fields:
\begin{description}
\item[\field{hdr}] specifies a request type (VIRTIO_SND_R_JACK_*).
\item[\field{jack_id}] specifies a jack identifier from 0 to \field{jacks} - 1.
\end{description}
\paragraph{VIRTIO_SND_R_JACK_INFO}
Query information about the available jacks.
The request consists of the virtio_snd_query_info structure
(see \nameref{sec:Device Types / Sound Device / Device Operation / Item Information Request}).
The response consists of the virtio_snd_hdr structure, followed by the following
jack information structures:
\begin{lstlisting}
/* supported jack features */
enum {
VIRTIO_SND_JACK_F_REMAP = 0
};
struct virtio_snd_jack_info {
struct virtio_snd_info hdr;
le32 features; /* 1 << VIRTIO_SND_JACK_F_XXX */
le32 hda_reg_defconf;
le32 hda_reg_caps;
u8 connected;
u8 padding[7];
};
\end{lstlisting}
The structure contains the following device-writable fields:
\begin{description}
\item[\field{features}] indicates a supported feature bit map:
\begin{itemize}
\item VIRTIO_SND_JACK_F_REMAP - jack remapping support.
\end{itemize}
\item[\field{hda_reg_defconf}] indicates a pin default configuration value
(see \hyperref[intro:HDA]{HDA}, section 7.3.3.31).
\item[\field{hda_reg_caps}] indicates a pin capabilities value
(see \hyperref[intro:HDA]{HDA}, section 7.3.4.9).
\item[\field{connected}] indicates the current jack connection status
(1 - connected, 0 - disconnected).
\end{description}
\devicenormative{\subparagraph}{Jack Information}{Device Types / Sound Device / Device Operation / Jack Information}
\begin{itemize}
\item The device MUST NOT set undefined feature values.
\item The device MUST initialize the \field{padding} bytes to 0.
\end{itemize}
\paragraph{VIRTIO_SND_R_JACK_REMAP}
If the VIRTIO_SND_JACK_F_REMAP feature bit is set in the jack information,
then the driver can send a control request to change the association and/or
sequence number for the specified jack ID.
The request uses the following structure and layout definitions:
\begin{lstlisting}
struct virtio_snd_jack_remap {
struct virtio_snd_jack_hdr hdr; /* .code = VIRTIO_SND_R_JACK_REMAP */
le32 association;
le32 sequence;
};
\end{lstlisting}
The request contains the following device-readable fields:
\begin{description}
\item[\field{association}] specifies the selected association number.
\item[\field{sequence}] specifies the selected sequence number.
\end{description}
\subsubsection{Jack Notifications}
Jack notifications consist of a virtio_snd_event structure, which has the following
device-writable fields:
\begin{description}
\item[\field{hdr}] indicates a jack event type:
\begin{itemize}
\item VIRTIO_SND_EVT_JACK_CONNECTED - an external device has been connected to the jack.
\item VIRTIO_SND_EVT_JACK_DISCONNECTED - an external device has been disconnected from the jack.
\end{itemize}
\item[\field{data}] indicates a jack identifier from 0 to \field{jacks} - 1.
\end{description}
\subsubsection{PCM Control Messages}\label{sec:Device Types / Sound Device / Device Operation / PCM Control Messages}
A PCM control request has, or consists of, a common header with the following
layout structure:
\begin{lstlisting}
struct virtio_snd_pcm_hdr {
struct virtio_snd_hdr hdr;
le32 stream_id;
};
\end{lstlisting}
The header consists of the following device-readable fields:
\begin{description}
\item[\field{hdr}] specifies request type (VIRTIO_SND_R_PCM_*).
\item[\field{stream_id}] specifies a PCM stream identifier from 0 to \field{streams} - 1.
\end{description}
\paragraph{PCM Command Lifecycle}
A PCM stream has the following command lifecycle:
\begin{enumerate}
\item SET PARAMETERS
The driver negotiates the stream parameters (format, transport, etc) with
the device.
Possible valid transitions: set parameters, prepare.
\item PREPARE
The device prepares the stream (allocates resources, etc).
Possible valid transitions: set parameters, prepare, start, release.
\item Output only: the driver transfers data for pre-buffing.
\item START
The device starts the stream (unmute, putting into running state, etc).
Possible valid transitions: stop.
\item The driver transfers data to/from the stream.
\item STOP
The device stops the stream (mute, putting into non-running state, etc).
Possible valid transitions: start, release.
\item RELEASE
The device releases the stream (frees resources, etc).
Possible valid transitions: set parameters, prepare.
\end{enumerate}
\paragraph{VIRTIO_SND_R_PCM_INFO}
Query information about the available streams.
The request consists of the virtio_snd_query_info structure
(see \nameref{sec:Device Types / Sound Device / Device Operation / Item Information Request}).
The response consists of the virtio_snd_hdr structure, followed by the following
stream information structures:
\begin{lstlisting}
/* supported PCM stream features */
enum {
VIRTIO_SND_PCM_F_SHMEM_HOST = 0,
VIRTIO_SND_PCM_F_SHMEM_GUEST,
VIRTIO_SND_PCM_F_MSG_POLLING,
VIRTIO_SND_PCM_F_EVT_SHMEM_PERIODS,
VIRTIO_SND_PCM_F_EVT_XRUNS
};
/* supported PCM sample formats */
enum {
/* analog formats (width / physical width) */
VIRTIO_SND_PCM_FMT_IMA_ADPCM = 0, /* 4 / 4 bits */
VIRTIO_SND_PCM_FMT_MU_LAW, /* 8 / 8 bits */
VIRTIO_SND_PCM_FMT_A_LAW, /* 8 / 8 bits */
VIRTIO_SND_PCM_FMT_S8, /* 8 / 8 bits */
VIRTIO_SND_PCM_FMT_U8, /* 8 / 8 bits */
VIRTIO_SND_PCM_FMT_S16, /* 16 / 16 bits */
VIRTIO_SND_PCM_FMT_U16, /* 16 / 16 bits */
VIRTIO_SND_PCM_FMT_S18_3, /* 18 / 24 bits */
VIRTIO_SND_PCM_FMT_U18_3, /* 18 / 24 bits */
VIRTIO_SND_PCM_FMT_S20_3, /* 20 / 24 bits */
VIRTIO_SND_PCM_FMT_U20_3, /* 20 / 24 bits */
VIRTIO_SND_PCM_FMT_S24_3, /* 24 / 24 bits */
VIRTIO_SND_PCM_FMT_U24_3, /* 24 / 24 bits */
VIRTIO_SND_PCM_FMT_S20, /* 20 / 32 bits */
VIRTIO_SND_PCM_FMT_U20, /* 20 / 32 bits */
VIRTIO_SND_PCM_FMT_S24, /* 24 / 32 bits */
VIRTIO_SND_PCM_FMT_U24, /* 24 / 32 bits */
VIRTIO_SND_PCM_FMT_S32, /* 32 / 32 bits */
VIRTIO_SND_PCM_FMT_U32, /* 32 / 32 bits */
VIRTIO_SND_PCM_FMT_FLOAT, /* 32 / 32 bits */
VIRTIO_SND_PCM_FMT_FLOAT64, /* 64 / 64 bits */
/* digital formats (width / physical width) */
VIRTIO_SND_PCM_FMT_DSD_U8, /* 8 / 8 bits */
VIRTIO_SND_PCM_FMT_DSD_U16, /* 16 / 16 bits */
VIRTIO_SND_PCM_FMT_DSD_U32, /* 32 / 32 bits */
VIRTIO_SND_PCM_FMT_IEC958_SUBFRAME /* 32 / 32 bits */
};
/* supported PCM frame rates */
enum {
VIRTIO_SND_PCM_RATE_5512 = 0,
VIRTIO_SND_PCM_RATE_8000,
VIRTIO_SND_PCM_RATE_11025,
VIRTIO_SND_PCM_RATE_16000,
VIRTIO_SND_PCM_RATE_22050,
VIRTIO_SND_PCM_RATE_32000,
VIRTIO_SND_PCM_RATE_44100,
VIRTIO_SND_PCM_RATE_48000,
VIRTIO_SND_PCM_RATE_64000,
VIRTIO_SND_PCM_RATE_88200,
VIRTIO_SND_PCM_RATE_96000,
VIRTIO_SND_PCM_RATE_176400,
VIRTIO_SND_PCM_RATE_192000,
VIRTIO_SND_PCM_RATE_384000
};
struct virtio_snd_pcm_info {
struct virtio_snd_info hdr;
le32 features; /* 1 << VIRTIO_SND_PCM_F_XXX */
le64 formats; /* 1 << VIRTIO_SND_PCM_FMT_XXX */
le64 rates; /* 1 << VIRTIO_SND_PCM_RATE_XXX */
u8 direction;
u8 channels_min;
u8 channels_max;
u8 padding[5];
};
\end{lstlisting}
The structure contains the following device-writable fields:
\begin{description}
\item[\field{features}] indicates a bit map of the supported features, which can
be negotiated by setting the stream parameters:
\begin{itemize}
\item VIRTIO_SND_PCM_F_SHMEM_HOST - supports sharing a host memory with a guest,
\item VIRTIO_SND_PCM_F_SHMEM_GUEST - supports sharing a guest memory with a host,
\item VIRTIO_SND_PCM_F_MSG_POLLING - supports polling mode for message-based
transport,
\item VIRTIO_SND_PCM_F_EVT_SHMEM_PERIODS - supports elapsed period notifications
for shared memory transport,
\item VIRTIO_SND_PCM_F_EVT_XRUNS - supports underrun/overrun notifications.
\end{itemize}
\item[\field{formats}] indicates a supported sample format bit map.
\item[\field{rates}] indicates a supported frame rate bit map.
\item[\field{direction}] indicates the direction of data flow (VIRTIO_SND_D_*).
\item[\field{channels_min}] indicates a minimum number of supported channels.
\item[\field{channels_max}] indicates a maximum number of supported channels.
\end{description}
Only interleaved samples are supported.
\devicenormative{\subparagraph}{Stream Information}{Device Types / Sound Device / Device Operation / PCM Stream Information}
\begin{itemize}
\item The device MUST NOT set undefined feature, format, rate and direction
values.
\item The device MUST initialize the \field{padding} bytes to 0.
\end{itemize}
\paragraph{VIRTIO_SND_R_PCM_SET_PARAMS}\label{sec:Device Types / Sound Device / Device Operation / PCM Stream Parameters}
Set selected stream parameters for the specified stream ID.
The request uses the following structure and layout definitions:
\begin{lstlisting}
struct virtio_snd_pcm_set_params {
struct virtio_snd_pcm_hdr hdr; /* .code = VIRTIO_SND_R_PCM_SET_PARAMS */
le32 buffer_bytes;
le32 period_bytes;
le32 features; /* 1 << VIRTIO_SND_PCM_F_XXX */
u8 channels;
u8 format;
u8 rate;
u8 padding;
};
\end{lstlisting}
The request contains the following device-readable fields:
\begin{description}
\item[\field{buffer_bytes}] specifies the size of the hardware buffer used by
the driver.
\item[\field{period_bytes}] specifies the size of the hardware period used by
the driver.
\item[\field{features}] specifies a selected feature bit map:
\begin{itemize}
\item VIRTIO_SND_PCM_F_SHMEM_HOST - use shared memory allocated by the host
(is a placeholder and MUST NOT be selected at the moment),
\item VIRTIO_SND_PCM_F_SHMEM_GUEST - use shared memory allocated by the guest
(is a placeholder and MUST NOT be selected at the moment),
\item VIRTIO_SND_PCM_F_MSG_POLLING - suppress available buffer notifications
for tx and rx queues (device should poll virtqueue),
\item VIRTIO_SND_PCM_F_EVT_SHMEM_PERIODS - enable elapsed period notifications
for shared memory transport,
\item VIRTIO_SND_PCM_F_EVT_XRUNS - enable underrun/overrun notifications.
\end{itemize}
\item[\field{channels}] specifies a selected number of channels.
\item[\field{format}] specifies a selected sample format (VIRTIO_SND_PCM_FMT_*).
\item[\field{rate}] specifies a selected frame rate (VIRTIO_SND_PCM_RATE_*).
\end{description}
\devicenormative{\subparagraph}{Stream Parameters}{Device Types / Sound Device / Device Operation / PCM Stream Parameters}
\begin{itemize}
\item If the device has an intermediate buffer, its size MUST be no less than
the specified \field{buffer_bytes} value.
\end{itemize}
\drivernormative{\subparagraph}{Stream Parameters}{Device Types / Sound Device / Device Operation / PCM Stream Parameters}
\begin{itemize}
\item \field{period_bytes} MUST be a divider \field{buffer_bytes}, i.e. \field{buffer_bytes} \% \field{period_bytes} == 0.
\item The driver MUST NOT set undefined feature, format and rate values.
\item The driver MUST NOT set the feature, format, and rate that are not specified
in the stream configuration.
\item The driver MUST NOT set the \field{channels} value as less than the \field{channels_min}
or greater than the \field{channels_max} values specified in the stream configuration.
\item The driver MUST NOT set the VIRTIO_SND_PCM_F_SHMEM_HOST and VIRTIO_SND_PCM_F_SHMEM_GUEST
bits at the same time.
\item The driver MUST initialize the \field{padding} byte to 0.
\item If the bits associated with the shared memory are not selected, the driver
MUST use the tx and rx queues for data transfer
(see \nameref{sec:Device Types / Sound Device / Device Operation / PCM IO Messages}).
\end{itemize}
\paragraph{VIRTIO_SND_R_PCM_PREPARE}
Prepare a stream with specified stream ID.
\paragraph{VIRTIO_SND_R_PCM_RELEASE}
Release a stream with specified stream ID.
\devicenormative{\subparagraph}{Stream Release}{Device Types / Sound Device / Device Operation / PCM Stream Release}
\begin{itemize}
\item The device MUST complete all pending I/O messages for the specified
stream ID.
\item The device MUST NOT complete the control request while there are pending
I/O messages for the specified stream ID.
\end{itemize}
\paragraph{VIRTIO_SND_R_PCM_START}
Start a stream with specified stream ID.
\paragraph{VIRTIO_SND_R_PCM_STOP}
Stop a stream with specified stream ID.
\subsubsection{PCM Notifications}
The device can announce support for different PCM events using feature bits
in the stream information structure. To enable notifications, the driver
must negotiate these features using the set stream parameters request
(see \ref{sec:Device Types / Sound Device / Device Operation / PCM Stream Parameters}).
PCM stream notifications consist of a virtio_snd_event structure, which has the
following device-writable fields:
\begin{description}
\item[\field{hdr}] indicates a PCM stream event type:
\begin{itemize}
\item VIRTIO_SND_EVT_PCM_PERIOD_ELAPSED - a hardware buffer period has elapsed,
the period size is controlled using the \field{period_bytes} field.
\item VIRTIO_SND_EVT_PCM_XRUN - an underflow for the output stream or an overflow
for the input stream has occurred.
\end{itemize}
\item[\field{data}] indicates a PCM stream identifier from 0 to \field{streams} - 1.
\end{description}
\subsubsection{PCM I/O Messages}\label{sec:Device Types / Sound Device / Device Operation / PCM IO Messages}
An I/O message consists of the header part, followed by the buffer, and then
the status part.
\begin{lstlisting}
/* an I/O header */
struct virtio_snd_pcm_xfer {
le32 stream_id;
};
/* an I/O status */
struct virtio_snd_pcm_status {
le32 status;
le32 latency_bytes;
};
\end{lstlisting}
The header part consists of the following device-readable field:
\begin{description}
\item[\field{stream_id}] specifies a PCM stream identifier from 0 to \field{streams} - 1.
\end{description}
The status part consists of the following device-writable fields:
\begin{description}
\item[\field{status}] contains VIRTIO_SND_S_OK if an operation is successful,
and VIRTIO_SND_S_IO_ERR otherwise.
\item[\field{latency_bytes}] indicates the current device latency.
\end{description}
Since all buffers in the queue (with one exception) should be of the size
\field{period_bytes}, the completion of such an I/O request can be considered an
elapsed period notification.
\paragraph{Output Stream}
In case of an output stream, the header is followed by a device-readable buffer
containing PCM frames for writing to the device. All messages are placed into
the tx queue.
\devicenormative{\subparagraph}{Output Stream}{Device Types / Sound Device / Device Operation / PCM Output Stream}
\begin{itemize}
\item The device MUST NOT complete the I/O request until the buffer is totally
consumed.
\end{itemize}
\drivernormative{\subparagraph}{Output Stream}{Device Types / Sound Device / Device Operation / PCM Output Stream}
\begin{itemize}
\item The driver SHOULD populate the tx queue with \field{period_bytes} sized
buffers. The only exception is the end of the stream.
\item The driver MUST NOT place device-writable buffers into the tx queue.
\end{itemize}
\paragraph{Input Stream}
In case of an input stream, the header is followed by a device-writable buffer
being populated with PCM frames from the device. All messages are placed into
the rx queue.
A used descriptor specifies the length of the buffer that was written by the
device. It should be noted that the length value contains the size of the
virtio_snd_pcm_status structure plus the size of the recorded frames.
\devicenormative{\subparagraph}{Input Stream}{Device Types / Sound Device / Device Operation / PCM Input Stream}
\begin{itemize}
\item The device MUST NOT complete the I/O request until the buffer is full.
The only exception is the end of the stream.
\end{itemize}
\drivernormative{\subparagraph}{Input Stream}{Device Types / Sound Device / Device Operation / PCM Input Stream}
\begin{itemize}
\item The driver SHOULD populate the rx queue with \field{period_bytes} sized
empty buffers before starting the stream.
\item The driver MUST NOT place device-readable buffers into the rx queue.
\end{itemize}
\subsubsection{Channel Map Control Messages}\label{sec:Device Types / Sound Device / Device Operation / Channel Map Control Messages}
A device can provide one or more channel maps assigned to all streams with
the same data flow direction in the same function group.
\paragraph{VIRTIO_SND_R_CHMAP_INFO}
Query information about the available channel maps.
The request consists of the virtio_snd_query_info structure
(see \nameref{sec:Device Types / Sound Device / Device Operation / Item Information Request}).
The response consists of the virtio_snd_hdr structure, followed by the following
channel map information structures:
\begin{lstlisting}
/* standard channel position definition */
enum {
VIRTIO_SND_CHMAP_NONE = 0, /* undefined */
VIRTIO_SND_CHMAP_NA, /* silent */
VIRTIO_SND_CHMAP_MONO, /* mono stream */
VIRTIO_SND_CHMAP_FL, /* front left */
VIRTIO_SND_CHMAP_FR, /* front right */
VIRTIO_SND_CHMAP_RL, /* rear left */
VIRTIO_SND_CHMAP_RR, /* rear right */
VIRTIO_SND_CHMAP_FC, /* front center */
VIRTIO_SND_CHMAP_LFE, /* low frequency (LFE) */
VIRTIO_SND_CHMAP_SL, /* side left */
VIRTIO_SND_CHMAP_SR, /* side right */
VIRTIO_SND_CHMAP_RC, /* rear center */
VIRTIO_SND_CHMAP_FLC, /* front left center */
VIRTIO_SND_CHMAP_FRC, /* front right center */
VIRTIO_SND_CHMAP_RLC, /* rear left center */
VIRTIO_SND_CHMAP_RRC, /* rear right center */
VIRTIO_SND_CHMAP_FLW, /* front left wide */
VIRTIO_SND_CHMAP_FRW, /* front right wide */
VIRTIO_SND_CHMAP_FLH, /* front left high */
VIRTIO_SND_CHMAP_FCH, /* front center high */
VIRTIO_SND_CHMAP_FRH, /* front right high */
VIRTIO_SND_CHMAP_TC, /* top center */
VIRTIO_SND_CHMAP_TFL, /* top front left */
VIRTIO_SND_CHMAP_TFR, /* top front right */
VIRTIO_SND_CHMAP_TFC, /* top front center */
VIRTIO_SND_CHMAP_TRL, /* top rear left */
VIRTIO_SND_CHMAP_TRR, /* top rear right */
VIRTIO_SND_CHMAP_TRC, /* top rear center */
VIRTIO_SND_CHMAP_TFLC, /* top front left center */
VIRTIO_SND_CHMAP_TFRC, /* top front right center */
VIRTIO_SND_CHMAP_TSL, /* top side left */
VIRTIO_SND_CHMAP_TSR, /* top side right */
VIRTIO_SND_CHMAP_LLFE, /* left LFE */
VIRTIO_SND_CHMAP_RLFE, /* right LFE */
VIRTIO_SND_CHMAP_BC, /* bottom center */
VIRTIO_SND_CHMAP_BLC, /* bottom left center */
VIRTIO_SND_CHMAP_BRC /* bottom right center */
};
/* maximum possible number of channels */
#define VIRTIO_SND_CHMAP_MAX_SIZE 18
struct virtio_snd_chmap_info {
struct virtio_snd_info hdr;
u8 direction;
u8 channels;
u8 positions[VIRTIO_SND_CHMAP_MAX_SIZE];
};
\end{lstlisting}
The structure contains the following device-writable fields:
\begin{description}
\item[\field{direction}] indicates the direction of data flow (VIRTIO_SND_D_*).
\item[\field{channels}] indicates the number of valid channel position values.
\item[\field{positions}] indicates channel position values (VIRTIO_SND_CHMAP_*).
\end{description}
\devicenormative{\subparagraph}{Channel Map Information}{Device Types / Sound Device / Device Operation / Channel Map Information}
\begin{itemize}
\item The device MUST NOT set undefined direction values.
\item The device MUST NOT set the channels value to more than VIRTIO_SND_CHMAP_MAX_SIZE.
\end{itemize}
\subsubsection{Control Elements}\label{sec:Device Types / Sound Device / Device Operation / Control Elements}
Control elements can be used to set the volume level, mute/unmute the audio
signal, switch different modes/states of the virtual sound device, etc. Design
of virtual audio controls is based on and derived from ALSA audio controls.
The device informs about the support of audio controls by setting the
VIRTIO_SND_F_CTLS feature bit. If VIRTIO_SND_F_CTLS has been negotiated, the
following messages are available for manipulation of control elements.
A control request has, or consists of, a common header with the following
layout structure:
\begin{lstlisting}
struct virtio_snd_ctl_hdr {
struct virtio_snd_hdr hdr;
le32 control_id;
};
\end{lstlisting}
The header consists of the following device-readable fields:
\begin{description}
\item[\field{hdr}] specifies request type (VIRTIO_SND_R_CTL_*).
\item[\field{control_id}] specifies a control element identifier from 0 to
\field{virtio_snd_config::controls} - 1.
\end{description}
\paragraph{Query information}
The VIRTIO_SND_R_CTL_INFO control message is used to query basic information
about the available control elements.
The request consists of the virtio_snd_query_info structure
(see \nameref{sec:Device Types / Sound Device / Device Operation / Item Information Request}).
The response consists of the virtio_snd_hdr structure, followed by the following
control element information structures:
\begin{lstlisting}
enum {
VIRTIO_SND_CTL_ROLE_UNDEFINED = 0,
VIRTIO_SND_CTL_ROLE_VOLUME,
VIRTIO_SND_CTL_ROLE_MUTE,
VIRTIO_SND_CTL_ROLE_GAIN
};
enum {
VIRTIO_SND_CTL_TYPE_BOOLEAN = 0,
VIRTIO_SND_CTL_TYPE_INTEGER,
VIRTIO_SND_CTL_TYPE_INTEGER64,
VIRTIO_SND_CTL_TYPE_ENUMERATED,
VIRTIO_SND_CTL_TYPE_BYTES,
VIRTIO_SND_CTL_TYPE_IEC958
};
enum {
VIRTIO_SND_CTL_ACCESS_READ = 0,
VIRTIO_SND_CTL_ACCESS_WRITE,
VIRTIO_SND_CTL_ACCESS_VOLATILE,
VIRTIO_SND_CTL_ACCESS_INACTIVE,
VIRTIO_SND_CTL_ACCESS_TLV_READ,
VIRTIO_SND_CTL_ACCESS_TLV_WRITE,
VIRTIO_SND_CTL_ACCESS_TLV_COMMAND
};
struct virtio_snd_ctl_info {
struct virtio_snd_info hdr;
le32 role;
le32 type;
le32 access; /* 1 << VIRTIO_SND_CTL_ACCESS_XXX */
le32 count;
le32 index;
u8 name[44];
union {
struct {
le32 min;
le32 max;
le32 step;
} integer;
struct {
le64 min;
le64 max;
le64 step;
} integer64;
struct {
le32 items;
} enumerated;
} value;
};
\end{lstlisting}
The structure contains the following device-writable fields:
\begin{description}
\item[\field{role}] indicates a role for the element. If the field value
is not equal to UNDEFINED, then the least significant bit indicates the direction
of data flow (VIRTIO_SND_D_*), and (\field{role} \& 0xfffffffe) >> 1 is equal to
one of the following values (VIRTIO_SND_CTL_ROLE_*):
\begin{description}
\item[VOLUME] is for a volume control.
\item[MUTE] is for a mute switch.
\item[GAIN] is for a gain control.
\end{description}
\item[\field{type}] indicates the element value type (VIRTIO_SND_CTL_TYPE_*):
\begin{description}
\item[BOOLEAN] This is a special case of the INTEGER type, which can take only
two values: 0 (off) and 1 (on).
\item[INTEGER] 32-bit integer values.
\item[INTEGER64] 64-bit integer values.
\item[ENUMERATED] The value is represented by an array of ASCII strings.
\item[BYTES] 8-bit integer values.
\item[IEC958] This element is connected to the digital audio interface.
The value is in the form of the virtio_snd_ctl_iec958 structure.
\end{description}
\item[\field{access}] indicates a bit mask describing access rights to the
element (VIRTIO_SND_CTL_ACCESS_*):
\begin{description}
\item[READ] It is possible to read the value.
\item[WRITE] It is possible to write (change) the value.
\item[VOLATILE] The value may be changed without a notification.
\item[INACTIVE] The element does actually nothing, but may be updated.
\item[TLV_READ] It is possible to read metadata.
\item[TLV_WRITE] It is possible to write (change) metadata.
\item[TLV_COMMAND] It is possible to execute a command for metadata.
\end{description}
\item[\field{count}] indicates the number of \field{type} members that represent
the value of the element.
\item[\field{index}] indicates the index for an element with a non-unique \field{name}.
\item[\field{name}] indicates the name identifier string for the element.
\item[\field{value}] indicates some additional information about the value for
certain types of elements:
\begin{description}
\item[\field{integer}]
\item[\field{integer64}] \field{min} and \field{max} indicate minimum and maximum
element values. \field{step} indicates a fixed step size for changing the element
value between minimum and maximum values. The special value 0 means that the step
has variable size.
\item[\field{enumerated}] \field{items} indicates the number of items from which
the element value can be selected.
\end{description}
\end{description}
To query an array of items for elements with the ENUMERATED type, an additional
VIRTIO_SND_R_CTL_ENUM_ITEMS control message is used. The request consists of the
virtio_snd_ctl_hdr structure. The response consists of the virtio_snd_hdr structure,
followed by an array of size \field{value.enumerated.items}, consisting of the following
structures:
\begin{lstlisting}
struct virtio_snd_ctl_enum_item {
u8 item[64];
};
\end{lstlisting}
The structure contains the only device-writable field:
\begin{description}
\item[\field{item}] indicates the name of an available element option.
\end{description}
\devicenormative{\subparagraph}{Control Element Information}{Device Types / Sound Device / Device Operation / Control Element Information}
\begin{itemize}
\item The device MUST NOT set undefined \field{role}, \field{type} and
\field{access} values.
\item The device MUST set the \field{count} to a value other than zero. The maximum
allowed value depends on the element type:
\begin{description}
\item[BOOLEAN] 128