firewire-digi00x-protocols 0.2.0

Implementation of protocol for Digi 00x family
Documentation
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
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright (c) 2021 Takashi Sakamoto

#![doc = include_str!("../README.md")]

use glib::{Error, FileError};

use hinawa::{prelude::FwReqExtManual, FwNode, FwReq, FwTcode};

/// The protocol implementation for Digi 002.
#[derive(Default, Debug)]
pub struct Digi002Protocol;

impl Dg00xHardwareSpecification for Digi002Protocol {
    const SAMPLING_CLOCK_SOURCES: &'static [ClockSource] =
        &[ClockSource::Internal, ClockSource::Spdif, ClockSource::Adat];
}

/// The protocol implementation for Digi 003.
#[derive(Default, Debug)]
pub struct Digi003Protocol;

impl Dg00xHardwareSpecification for Digi003Protocol {
    const SAMPLING_CLOCK_SOURCES: &'static [ClockSource] = &[
        ClockSource::Internal,
        ClockSource::Spdif,
        ClockSource::Adat,
        ClockSource::WordClock,
    ];
}

const BASE_OFFSET: u64 = 0xffffe0000000;

fn read_quadlet(
    req: &mut FwReq,
    node: &mut FwNode,
    offset: u64,
    timeout_ms: u32,
) -> Result<u32, Error> {
    let mut quadlet = [0; 4];
    req.transaction_sync(
        node,
        FwTcode::ReadQuadletRequest,
        BASE_OFFSET + offset,
        quadlet.len(),
        &mut quadlet,
        timeout_ms,
    )
    .map(|_| u32::from_be_bytes(quadlet))
}

fn write_quadlet(
    req: &mut FwReq,
    node: &mut FwNode,
    offset: u64,
    val: u32,
    timeout_ms: u32,
) -> Result<(), Error> {
    let mut quadlet = [0; 4];
    quadlet.copy_from_slice(&val.to_be_bytes());
    req.transaction_sync(
        node,
        FwTcode::WriteQuadletRequest,
        BASE_OFFSET + offset,
        quadlet.len(),
        &mut quadlet,
        timeout_ms,
    )
}

/// The specification of hardware.
pub trait Dg00xHardwareSpecification {
    const SAMPLING_CLOCK_SOURCES: &'static [ClockSource];
    const SAMPLING_CLOCK_RATES: &'static [u32] = &[44100, 48000, 88200, 96000];

    const MONITOR_SOURCE_GAIN_MIN: u8 = 0;
    const MONITOR_SOURCE_GAIN_MAX: u8 = 0x80;
    const MONITOR_SOURCE_GAIN_STEP: u8 = 1;
}

/// Cache whole parameters.
pub trait Dg00xWhollyCachableParamsOperation<T>: Dg00xHardwareSpecification {
    fn cache_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        states: &mut T,
        timeout_ms: u32,
    ) -> Result<(), Error>;
}

/// Update the part of parameters.
pub trait Dg00xPartiallyUpdatableParamsOperation<T>: Dg00xHardwareSpecification {
    fn update_partially(
        req: &mut FwReq,
        node: &mut FwNode,
        params: &mut T,
        update: T,
        timeout_ms: u32,
    ) -> Result<(), Error>;
}

/// Update whole parameters.
pub trait Dg00xWhollyUpdatableParamsOperation<T>: Dg00xHardwareSpecification {
    fn update_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        states: &T,
        timeout_ms: u32,
    ) -> Result<(), Error>;
}

/// Nominal frequency of media clock.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ClockRate {
    R44100,
    R48000,
    R88200,
    R96000,
}

impl Default for ClockRate {
    fn default() -> Self {
        Self::R44100
    }
}

/// Signal source of sampling clock.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ClockSource {
    Internal,
    Spdif,
    Adat,
    WordClock,
}

impl Default for ClockSource {
    fn default() -> Self {
        Self::Internal
    }
}

const SAMPLING_CLOCK_SOURCE_OFFSET: u64 = 0x0118;
const MEDIA_CLOCK_RATE_OFFSET: u64 = 0x0110;
const EXTERNAL_CLOCK_RATE_OFFSET: u64 = 0x0114;
const OPTICAL_INTERFACE_MODE_OFFSET: u64 = 0x011c;
const EXTERNAL_CLOCK_SOURCE_DETECTION_OFFSET: u64 = 0x012c;

/// The parameters for sampling clock.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub struct Dg00xSamplingClockParameters {
    /// The source.
    pub source: ClockSource,
}

impl<O> Dg00xWhollyCachableParamsOperation<Dg00xSamplingClockParameters> for O
where
    O: Dg00xHardwareSpecification,
{
    fn cache_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        states: &mut Dg00xSamplingClockParameters,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        read_quadlet(req, node, SAMPLING_CLOCK_SOURCE_OFFSET, timeout_ms).and_then(|val| {
            let pos = val as usize;
            Self::SAMPLING_CLOCK_SOURCES
                .iter()
                .nth(pos)
                .ok_or_else(|| {
                    let msg = format!("Unexpected clock source: {}", pos);
                    Error::new(FileError::Io, &msg)
                })
                .map(|&s| states.source = s)
        })
    }
}

impl<O> Dg00xWhollyUpdatableParamsOperation<Dg00xSamplingClockParameters> for O
where
    O: Dg00xHardwareSpecification,
{
    fn update_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        params: &Dg00xSamplingClockParameters,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let pos = Self::SAMPLING_CLOCK_SOURCES
            .iter()
            .position(|&s| s.eq(&params.source))
            .ok_or_else(|| {
                let msg = format!("Invalid argument for clock source: {:?}", params.source);
                Error::new(FileError::Inval, &msg)
            })?;
        let val = pos as u32;
        write_quadlet(req, node, SAMPLING_CLOCK_SOURCE_OFFSET, val, timeout_ms)
    }
}

/// The parameters for media clock.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub struct Dg00xMediaClockParameters {
    /// The rate.
    pub rate: ClockRate,
}

const CLOCK_RATE_TABLE: &[ClockRate] = &[
    ClockRate::R44100,
    ClockRate::R48000,
    ClockRate::R88200,
    ClockRate::R96000,
];

fn serialize_clock_rate(rate: &ClockRate) -> u32 {
    CLOCK_RATE_TABLE
        .iter()
        .position(|r| rate.eq(r))
        .map(|pos| pos as u32)
        .unwrap()
}

fn deserialize_clock_rate(rate: &mut ClockRate, val: u32) -> Result<(), Error> {
    *rate = match val {
        0 => Ok(ClockRate::R44100),
        1 => Ok(ClockRate::R48000),
        2 => Ok(ClockRate::R88200),
        3 => Ok(ClockRate::R96000),
        _ => {
            let msg = format!("Unexpected value for clock rate: {}", val);
            Err(Error::new(FileError::Inval, &msg))
        }
    }?;
    Ok(())
}

impl<O> Dg00xWhollyCachableParamsOperation<Dg00xMediaClockParameters> for O
where
    O: Dg00xHardwareSpecification,
{
    fn cache_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        states: &mut Dg00xMediaClockParameters,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        read_quadlet(req, node, MEDIA_CLOCK_RATE_OFFSET, timeout_ms)
            .and_then(|val| deserialize_clock_rate(&mut states.rate, val))
    }
}

impl<O> Dg00xWhollyUpdatableParamsOperation<Dg00xMediaClockParameters> for O
where
    O: Dg00xHardwareSpecification,
{
    fn update_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        params: &Dg00xMediaClockParameters,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let val = serialize_clock_rate(&params.rate);
        write_quadlet(req, node, SAMPLING_CLOCK_SOURCE_OFFSET, val, timeout_ms)
    }
}

/// Mode of optical interface.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum OpticalInterfaceMode {
    Adat,
    Spdif,
}

impl Default for OpticalInterfaceMode {
    fn default() -> Self {
        Self::Adat
    }
}

impl Dg00xWhollyCachableParamsOperation<OpticalInterfaceMode> for Digi003Protocol {
    fn cache_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        states: &mut OpticalInterfaceMode,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        read_quadlet(req, node, OPTICAL_INTERFACE_MODE_OFFSET, timeout_ms).map(|val| {
            *states = if val > 0 {
                OpticalInterfaceMode::Spdif
            } else {
                OpticalInterfaceMode::Adat
            };
        })
    }
}

impl Dg00xWhollyUpdatableParamsOperation<OpticalInterfaceMode> for Digi003Protocol {
    fn update_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        params: &OpticalInterfaceMode,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let val = match params {
            OpticalInterfaceMode::Adat => 0,
            OpticalInterfaceMode::Spdif => 1,
        };
        write_quadlet(req, node, OPTICAL_INTERFACE_MODE_OFFSET, val, timeout_ms)
    }
}

/// The parameters for media clock.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub struct Dg00xExternalClockParameters {
    /// The rate detected in input of external source. Once the source of sampling clock is
    /// configured to any external source, the function can return detected frequency. Once losing
    /// the input, it returns None.
    pub rate: Option<ClockRate>,
}

impl<O> Dg00xWhollyCachableParamsOperation<Dg00xExternalClockParameters> for O
where
    O: Dg00xHardwareSpecification,
{
    fn cache_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        states: &mut Dg00xExternalClockParameters,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        let detected = read_quadlet(
            req,
            node,
            EXTERNAL_CLOCK_SOURCE_DETECTION_OFFSET,
            timeout_ms,
        )?;

        if detected > 0 {
            let val = read_quadlet(req, node, EXTERNAL_CLOCK_RATE_OFFSET, timeout_ms)?;
            let mut rate = ClockRate::default();
            deserialize_clock_rate(&mut rate, val).map(|_| states.rate = Some(rate))
        } else {
            states.rate = None;
            Ok(())
        }
    }
}

const MONITOR_DST_COUNT: usize = 2;
const MONITOR_SRC_COUNT: usize = 18;

/// State of monitor. At offline mode (no packet streaming runs), the monitor function is disabled
/// and is not configurable. When packet streaming starts, the monitor function becomes
/// configurable with reset state.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct Dg00xMonitorState {
    /// Whether to enable monitor mixer or not.
    pub enabled: bool,
    /// The gain of monitor inputs. The value is between 0x00 and 0x80 for -48.0 and 0.0 dB.
    pub src_gains: [[u8; MONITOR_SRC_COUNT]; MONITOR_DST_COUNT],
}

const ENABLE_OFFSET: u64 = 0x0124;
const MONITOR_SRC_GAIN_OFFSET: u64 = 0x0300;

const DST_STEP: usize = 4;
const SRC_STEP: usize = 8;

fn compute_source_offset(dst: usize, src: usize) -> u64 {
    (dst * DST_STEP + src * SRC_STEP) as u64
}

impl<O> Dg00xWhollyCachableParamsOperation<Dg00xMonitorState> for O
where
    O: Dg00xHardwareSpecification,
{
    fn cache_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        states: &mut Dg00xMonitorState,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        read_quadlet(req, node, ENABLE_OFFSET, timeout_ms).map(|val| states.enabled = val > 0)?;
        states
            .src_gains
            .iter_mut()
            .enumerate()
            .try_for_each(|(dst, gains)| {
                gains.iter_mut().enumerate().try_for_each(|(src, gain)| {
                    let offset = MONITOR_SRC_GAIN_OFFSET + compute_source_offset(dst, src);
                    read_quadlet(req, node, offset, timeout_ms).map(|val| *gain = (val >> 24) as u8)
                })
            })
    }
}

impl<O> Dg00xWhollyUpdatableParamsOperation<Dg00xMonitorState> for O
where
    O: Dg00xHardwareSpecification,
{
    fn update_wholly(
        req: &mut FwReq,
        node: &mut FwNode,
        states: &Dg00xMonitorState,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        write_quadlet(req, node, ENABLE_OFFSET, states.enabled as u32, timeout_ms)?;
        states
            .src_gains
            .iter()
            .enumerate()
            .try_for_each(|(dst, gains)| {
                gains.iter().enumerate().try_for_each(|(src, &gain)| {
                    let offset = MONITOR_SRC_GAIN_OFFSET + compute_source_offset(dst, src);
                    let val = (gain as u32) << 24;
                    write_quadlet(req, node, offset, val, timeout_ms)
                })
            })
    }
}

impl<O> Dg00xPartiallyUpdatableParamsOperation<Dg00xMonitorState> for O
where
    O: Dg00xHardwareSpecification,
{
    fn update_partially(
        req: &mut FwReq,
        node: &mut FwNode,
        states: &mut Dg00xMonitorState,
        updates: Dg00xMonitorState,
        timeout_ms: u32,
    ) -> Result<(), Error> {
        if states.enabled != updates.enabled {
            write_quadlet(req, node, ENABLE_OFFSET, updates.enabled as u32, timeout_ms)
                .map(|_| states.enabled = updates.enabled)?;
        }

        states
            .src_gains
            .iter_mut()
            .zip(updates.src_gains.iter())
            .enumerate()
            .try_for_each(|(dst, (state, update))| {
                state
                    .iter_mut()
                    .zip(update.iter())
                    .enumerate()
                    .filter(|(_, (o, n))| !o.eq(n))
                    .try_for_each(|(src, (g, &gain))| {
                        let offset = MONITOR_SRC_GAIN_OFFSET + compute_source_offset(dst, src);
                        let val = (gain as u32) << 24;
                        write_quadlet(req, node, offset, val, timeout_ms).map(|_| *g = gain)
                    })
            })
    }
}