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
//! TRB (Transfer Request Block).

use bit_field::BitField;
use core::convert::TryInto;
use num_derive::FromPrimitive;

macro_rules! reserved{
    ($name:ident($ty:expr) {
        $([$index:expr] $range:expr);*
    })=>{
        impl TryFrom<[u32;4]> for $name{
            type Error=[u32;4];

            fn try_from(raw:[u32;4])->Result<Self,Self::Error>{
                use crate::ring::trb::Type;

                $(if raw[$index].get_bits($range) != 0{
                    return Err(raw);
                })*

                if raw[3].get_bits(10..=15)!=$ty as _ {
                    return Err(raw);
                }

                Ok(Self(raw))
            }
        }
    }
}
macro_rules! add_trb {
    ($name:ident,$full:expr,$ty:expr) => {
        #[doc = $full ]
        #[repr(transparent)]
        #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
        pub struct $name([u32; 4]);
        impl $name {
            /// Returns the wrapped array.
            #[must_use]
            pub fn into_raw(self) -> [u32; 4] {
                self.0
            }

            /// Returns the value of the Cycle Bit.
            #[must_use]
            pub fn cycle_bit(&self) -> bool {
                self.0[3].get_bit(0)
            }

            /// Sets the value of the Cycle Bit.
            pub fn set_cycle_bit(&mut self, b: bool) -> &mut Self {
                use bit_field::BitField;
                self.0[3].set_bit(0, b);
                self
            }

            fn set_trb_type(&mut self) -> &mut Self {
                use crate::ring::trb::Type;
                use bit_field::BitField;
                self.0[3].set_bits(10..=15, $ty as _);
                self
            }
        }
        impl AsRef<[u32]> for $name {
            fn as_ref(&self) -> &[u32] {
                &self.0
            }
        }
        impl From<$name> for [u32; 4] {
            fn from(t: $name) -> Self {
                t.0
            }
        }
    };
}
macro_rules! impl_default_simply_adds_trb_id {
    ($name:ident,$full:expr) => {
        impl $name{
            paste::paste! {
                #[doc = "Creates a new " $full ".\n\nThis method sets the sets the value of the TRB Type field properly. All the other fieldds are set to 0."]
                #[must_use]
                pub fn new()->Self{
                    *Self([0;4]).set_trb_type()
                }
            }
        }
        impl Default for $name {
            fn default() -> Self {
                Self::new()
            }
        }
    };
}
macro_rules! add_trb_with_default {
    ($name:ident,$full:expr,$type:expr) => {
        add_trb!($name, $full, $type);
        impl_default_simply_adds_trb_id!($name, $full);
    };
}
macro_rules! impl_debug_for_trb{
    ($name:ident {
        $($method:ident),*
    })=>{
        impl_debug_from_methods!{
            $name {
                $($method,)*
                cycle_bit
            }
        }
    }
}

macro_rules! allowed {
    (
        $(#[$outer:meta])*
        enum {
            $($(#[$doc:meta])* $variant:ident),+
        }
    ) => {
        $(#[$outer])*
            #[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
            pub enum Allowed {
                $($(#[$doc])* $variant($variant)),+
            }
        impl Allowed{
            /// Sets the value of the Cycle Bit.
            pub fn set_cycle_bit(&mut self,b:bool)->&mut Self{
                match self{
                    $(
                        Self::$variant(ref mut v) => {
                            v.set_cycle_bit(b);
                        }
                    ),+
                }
                self
            }

            /// Returns the value of the Cycle Bit.
            #[must_use]
            pub fn cycle_bit(&self)->bool{
                match self{
                    $( Self::$variant(ref v) => v.cycle_bit() ),+
                }
            }

            /// Returns the wrapped array.
            #[must_use]
            pub fn into_raw(self)->[u32;4]{
                match self{
                    $( Self::$variant(v) => v.into_raw() ),+
                }
            }
        }
        impl AsRef<[u32]> for Allowed {
            fn as_ref(&self) -> &[u32]{
                match self{
                    $( Self::$variant(ref v) => v.as_ref() ),+
                }
            }
        }
        $(
            impl From<$variant> for Allowed{
                fn from(v:$variant)->Self{
                    Self::$variant(v)
                }
            }
        )+
    };
}

pub mod command;
pub mod event;
pub mod transfer;

/// The bytes of a TRB.
pub const BYTES: usize = 16;

add_trb_with_default!(Link, "Link TRB", Type::Link);
impl Link {
    /// Sets the value of the Ring Segment Pointer field.
    ///
    /// # Panics
    ///
    /// This method panics if `p` is not 16-byte aligned.
    pub fn set_ring_segment_pointer(&mut self, p: u64) -> &mut Self {
        assert_eq!(
            p % 16,
            0,
            "The Ring Segment Pointer must be 16-byte aligned."
        );

        let l = p.get_bits(0..32);
        let u = p.get_bits(32..64);

        self.0[0] = l.try_into().unwrap();
        self.0[1] = u.try_into().unwrap();
        self
    }

    /// Returns the value of the Ring Segment Pointer field.
    #[must_use]
    pub fn ring_segment_pointer(&self) -> u64 {
        let l: u64 = self.0[0].into();
        let u: u64 = self.0[1].into();

        (u << 32) | l
    }

    /// Sets the value of the Interrupter Target field.
    pub fn set_interrupter_target(&mut self, t: u32) -> &mut Self {
        self.0[2].set_bits(22..=31, t);
        self
    }

    /// Returns the value of the Interrupter Target field.
    #[must_use]
    pub fn interrupter_target(&self) -> u32 {
        self.0[2].get_bits(22..=31)
    }

    /// Sets the value of the Toggle Cycle field.
    pub fn set_toggle_cycle(&mut self, c: bool) -> &mut Self {
        self.0[3].set_bit(1, c);
        self
    }

    /// Returns the value of the Toggle Cycle field.
    #[must_use]
    pub fn toggle_cycle(&self) -> bool {
        self.0[3].get_bit(1)
    }

    /// Sets the value of the Chain bit field.
    pub fn set_chain_bit(&mut self, b: bool) -> &mut Self {
        self.0[3].set_bit(4, b);
        self
    }

    /// Returns the value of the Chain bit field.
    #[must_use]
    pub fn chain_bit(&self) -> bool {
        self.0[3].get_bit(4)
    }

    /// Sets the value of the Interrupt On Completion field.
    pub fn set_interrupt_on_completion(&mut self, ioc: bool) -> &mut Self {
        self.0[3].set_bit(5, ioc);
        self
    }

    /// Returns the value of the Interrupt On Completion field.
    #[must_use]
    pub fn interrupt_on_completion(&self) -> bool {
        self.0[3].get_bit(5)
    }
}
impl_debug_for_trb!(Link {
    ring_segment_pointer,
    interrupter_target,
    toggle_cycle,
    chain_bit,
    interrupt_on_completion
});

/// TRB Type.
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, FromPrimitive)]
pub enum Type {
    /// Normal TRB, 1
    Normal = 1,
    /// Setup Stage TRB, 2
    SetupStage = 2,
    /// Data Stage TRB, 3
    DataStage = 3,
    /// Status Stage TRB, 4
    StatusStage = 4,
    /// Isoch TRB, 5
    Isoch = 5,
    /// Link TRB, 6
    Link = 6,
    /// Event Data TRB, 7
    EventData = 7,
    /// No Op TRB (Transfer), 8
    NoopTransfer = 8,
    /// Enable Slot Command TRB, 9
    EnableSlot = 9,
    /// Disable Slot Command TRB, 10
    DisableSlot = 10,
    /// Address Device Command TRB, 11
    AddressDevice = 11,
    /// Configure Endpoint Command TRB, 12
    ConfigureEndpoint = 12,
    /// Evaluate Context Command TRB, 13
    EvaluateContext = 13,
    /// Reset Endpoint Command TRB, 14
    ResetEndpoint = 14,
    /// Stop Endpoint Command TRB, 15
    StopEndpoint = 15,
    /// Set TR Dequeue Pointer Command TRB, 16
    SetTrDequeuePointer = 16,
    /// Reset Device Command TRB, 17
    ResetDevice = 17,
    /// Force Event Command TRB, 18
    ForceEvent = 18,
    /// Negotiate Bandwidth Command TRB, 19
    NegotiateBandwidth = 19,
    /// Set Latency Tolerance Value Command TRB, 20
    SetLatencyToleranceValue = 20,
    /// Get Port Bandwidth Command TRB, 21
    GetPortBandwidth = 21,
    /// Force Header Command TRB, 22
    ForceHeader = 22,
    /// No Op Command TRB, 23
    NoopCommand = 23,
    /// Get Extended Property Command TRB, 24
    GetExtendedProperty = 24,
    /// Set Extended Property Command TRB, 25
    SetExtendedProperty = 25,
    /// Transfer Event TRB, 32
    TransferEvent = 32,
    /// Command Completion Event TRB, 33
    CommandCompletion = 33,
    /// Port Status Change Event TRB, 34
    PortStatusChange = 34,
    /// Bandwidth Request Event TRB, 35
    BandwidthRequest = 35,
    /// Doorbell Event TRB, 36
    Doorbell = 36,
    /// Host Controller Event TRB, 37
    HostController = 37,
    /// Device Notification Event TRB, 38
    DeviceNotification = 38,
    /// MFINDEX Wrap Event TRB, 39
    MfindexWrap = 39,
}