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
// Copyright 2020 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use data_model::Le32;
use data_model::Le64;
use serde::Deserialize;
use serde::Serialize;
use zerocopy::AsBytes;
use zerocopy::FromBytes;
use zerocopy::FromZeroes;

use crate::virtio::snd::constants::StatusCode;
use crate::virtio::snd::constants::VIRTIO_SND_CHMAP_MAX_SIZE;

#[derive(Copy, Clone, Default, AsBytes, FromZeroes, FromBytes, Serialize, Deserialize)]
#[repr(C)]
pub struct virtio_snd_hdr {
    pub code: Le32,
}

#[derive(Copy, Clone, Default, AsBytes, FromZeroes, FromBytes)]
#[repr(C)]
pub struct virtio_snd_jack_hdr {
    pub hdr: virtio_snd_hdr,
    pub jack_id: Le32,
}

#[derive(Copy, Clone, Default, AsBytes, FromZeroes, FromBytes)]
#[repr(C)]
pub struct virtio_snd_event {
    pub hdr: virtio_snd_hdr,
    pub data: Le32,
}

#[derive(Copy, Clone, Default, AsBytes, FromZeroes, FromBytes)]
#[repr(C)]
pub struct virtio_snd_query_info {
    pub hdr: virtio_snd_hdr,
    pub start_id: Le32,
    pub count: Le32,
    pub size: Le32,
}

#[derive(
    Copy,
    Clone,
    Default,
    AsBytes,
    FromZeroes,
    FromBytes,
    Serialize,
    Deserialize,
    PartialEq,
    Eq,
    Debug,
)]
#[repr(C)]
pub struct virtio_snd_info {
    pub hda_fn_nid: Le32,
}

#[derive(
    Copy,
    Clone,
    Default,
    AsBytes,
    FromZeroes,
    FromBytes,
    Serialize,
    Deserialize,
    PartialEq,
    Eq,
    Debug,
)]
#[repr(C)]
pub struct virtio_snd_pcm_info {
    pub hdr: virtio_snd_info,
    pub features: Le32, /* 1 << VIRTIO_SND_PCM_F_XXX */
    pub formats: Le64,  /* 1 << VIRTIO_SND_PCM_FMT_XXX */
    pub rates: Le64,    /* 1 << VIRTIO_SND_PCM_RATE_XXX */
    pub direction: u8,
    pub channels_min: u8,
    pub channels_max: u8,

    pub padding: [u8; 5],
}

#[derive(Copy, Clone, Default, AsBytes, FromZeroes, FromBytes, Serialize, Deserialize)]
#[repr(C)]
pub struct virtio_snd_pcm_hdr {
    pub hdr: virtio_snd_hdr,
    pub stream_id: Le32,
}

#[derive(Copy, Clone, Default, AsBytes, FromZeroes, FromBytes, Serialize, Deserialize)]
#[repr(C)]
pub struct virtio_snd_pcm_set_params {
    pub hdr: virtio_snd_pcm_hdr,
    pub buffer_bytes: Le32,
    pub period_bytes: Le32,
    pub features: Le32, /* 1 << VIRTIO_SND_PCM_F_XXX */
    pub channels: u8,
    pub format: u8,
    pub rate: u8,
    pub padding: u8,
}

#[derive(Copy, Clone, AsBytes, FromZeroes, FromBytes)]
#[repr(C)]
pub struct virtio_snd_pcm_xfer {
    pub stream_id: Le32,
}

#[derive(Copy, Clone, Default, AsBytes, FromZeroes, FromBytes)]
#[repr(C)]
pub struct virtio_snd_pcm_status {
    pub status: Le32,
    pub latency_bytes: Le32,
}

impl virtio_snd_pcm_status {
    pub fn new(status: StatusCode, latency_bytes: u32) -> Self {
        Self {
            status: Le32::from(status as u32),
            latency_bytes: Le32::from(latency_bytes),
        }
    }
}

#[derive(
    Copy,
    Clone,
    Default,
    AsBytes,
    FromZeroes,
    FromBytes,
    Serialize,
    Deserialize,
    PartialEq,
    Eq,
    Debug,
)]
#[repr(C)]
pub struct virtio_snd_jack_info {
    pub hdr: virtio_snd_info,
    pub features: Le32, /* 1 << VIRTIO_SND_JACK_F_XXX */
    pub hda_reg_defconf: Le32,
    pub hda_reg_caps: Le32,
    pub connected: u8,
    pub padding: [u8; 7],
}

#[derive(Copy, Clone, Default, AsBytes, FromZeroes, FromBytes)]
#[repr(C)]
pub struct virtio_snd_jack_remap {
    pub hdr: virtio_snd_jack_hdr, /* .code = VIRTIO_SND_R_JACK_REMAP */
    pub association: Le32,
    pub sequence: Le32,
}

#[derive(
    Copy,
    Clone,
    Default,
    AsBytes,
    FromZeroes,
    FromBytes,
    Serialize,
    Deserialize,
    PartialEq,
    Eq,
    Debug,
)]
#[repr(C)]
pub struct virtio_snd_chmap_info {
    pub hdr: virtio_snd_info,
    pub direction: u8,
    pub channels: u8,
    pub positions: [u8; VIRTIO_SND_CHMAP_MAX_SIZE],
}