usbip-device 0.2.0

An implementation of usb-device on top of USBIP device.
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
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
pub(crate) mod cmd;
pub(crate) mod debug;
pub(crate) mod handler;
pub(crate) mod op;
pub(crate) mod request;
pub(crate) mod response;

use crate::{cmd::UsbIpHeader, handler::SocketHandler, request::UsbIpCmdSubmit};
use std::{
    collections::VecDeque,
    sync::{Arc, Mutex, MutexGuard},
};
use usb_device::{
    Result as UsbResult, UsbDirection, UsbError,
    {
        bus::{PollResult, UsbBus},
        endpoint::{EndpointAddress, EndpointType},
    },
};

#[derive(Debug, Clone)]
/// The error type, used by this crate.
pub enum UsbIpError {
    /// The connection closed unexpectedly.
    ConnectionClosed,

    /// A packet was received but it was shorter than the 48 bytes required to parse the header.
    PkgTooShort(usize),

    /// A received packet contained a command, that is unknown to the USBIP specification.
    InvalidCommand(u16),

    /// A received packet had a status field set to an unknown status value.
    StatusNotOk(u32),
}

impl std::fmt::Display for UsbIpError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::ConnectionClosed => write!(f, "connection no longer exsists"),
            Self::PkgTooShort(len) => write!(f, "packet of length {} is to short to parse", len),
            Self::InvalidCommand(cmd) => write!(f, "unknown command: {}", cmd),
            Self::StatusNotOk(status) => write!(f, "received invalid status: {}", status),
        }
    }
}

impl std::error::Error for UsbIpError {}

const NUM_ENDPOINTS: usize = 8;

#[derive(Debug, Clone)]
pub(crate) struct Pipe {
    pub data: VecDeque<Vec<u8>>,
    pub ty: EndpointType,
    pub max_packet_size: u16,
    #[allow(dead_code)]
    pub interval: u8,
}

impl Pipe {
    /// Checks, whether the endpoint contains a full transaction
    /// (terminated by a short packet) and is ready to send it.
    pub fn is_rts(&self) -> bool {
        match self.data.back() {
            // If there is no data pending, we are not ready to send
            None => false,
            Some(val) => {
                if self.ty != EndpointType::Control {
                    true
                } else {
                    // Control Endpoint use packet transactions.
                    // Therefore, we must wait until a transaction is complete before we return the packets
                    val.len() < self.max_packet_size as usize
                }
            }
        }
    }
}

#[derive(Debug, Clone)]
struct Endpoint {
    pub(crate) pipe_in: Option<Pipe>,
    pub(crate) pipe_out: Option<Pipe>,
    pub(crate) pending_ins: VecDeque<(UsbIpHeader, UsbIpCmdSubmit, Vec<u8>)>,
    pub(crate) stalled: bool,
    pub(crate) setup_flag: bool,
    pub(crate) in_complete_flag: bool,
}

impl Default for Endpoint {
    fn default() -> Self {
        Self {
            pipe_in: None,
            pipe_out: None,
            pending_ins: VecDeque::new(),
            stalled: true,
            setup_flag: false,
            in_complete_flag: false,
        }
    }
}

impl Endpoint {
    /// Returns the input pipe of this endpoint
    fn get_in(&mut self) -> UsbResult<&mut Pipe> {
        self.pipe_in.as_mut().ok_or(UsbError::InvalidEndpoint)
    }

    /// Returns the output pipe of this endpoint
    fn get_out(&mut self) -> UsbResult<&mut Pipe> {
        self.pipe_out.as_mut().ok_or(UsbError::InvalidEndpoint)
    }

    /// Checks, whether the input pipe is ready to send data back to the host.
    fn is_rts(&self) -> bool {
        match self.pipe_in {
            None => false,
            Some(ref pipe) => pipe.is_rts(),
        }
    }

    /// Processes an unlink and removes the pending packet on this endpoint.
    ///
    /// # Returns
    /// - `true` if pending urb was removed
    /// - `false` if it was not found
    // NOTE: This is super inefficient, use linked lists, as soon as linked_list_remove stabilizes
    fn unlink(&mut self, seqnum: u32) -> bool {
        let old_len = self.pending_ins.len();

        self.pending_ins = self
            .pending_ins
            .drain(..)
            .filter(|(header, _, _)| header.seqnum != seqnum)
            .collect();

        // If the length is the same as before, we have not changed anything
        // and return false
        old_len != self.pending_ins.len()
    }
}

#[derive(Debug)]
pub(crate) struct UsbIpBusInner {
    pub handler: SocketHandler,
    pub endpoint: [Endpoint; NUM_ENDPOINTS],
    pub device_address: u8,
    pub reset: bool,
    pub suspended: bool,
}

impl UsbIpBusInner {
    /// Creates a new UsbIpBusInner
    fn new() -> Self {
        Self {
            handler: SocketHandler::new(),
            endpoint: <[Endpoint; NUM_ENDPOINTS]>::default(),
            device_address: 0,
            reset: true,
            suspended: false,
        }
    }

    /// Resets the handler to the state, in which it acts like it is new
    fn reset(&mut self) {
        self.endpoint = <[Endpoint; NUM_ENDPOINTS]>::default();
        self.reset = true;
        self.suspended = false;
    }

    /// Returns the first enpoint, that is not already initialized or `None`,
    /// if all are already in use.
    fn next_available_endpoint(&self, direction: UsbDirection) -> Option<usize> {
        match direction {
            UsbDirection::In => {
                for i in 1..NUM_ENDPOINTS {
                    if self.endpoint[i].pipe_in.is_none() {
                        return Some(i);
                    }
                }
            }
            UsbDirection::Out => {
                for i in 1..NUM_ENDPOINTS {
                    if self.endpoint[i].pipe_out.is_none() {
                        return Some(i);
                    }
                }
            }
        }

        None
    }

    /// Returns the requested endpoint if it exists and
    /// [`UsbError::InvalidEndpoint`] otherwise.
    fn get_endpoint(&mut self, ep: usize) -> UsbResult<&mut Endpoint> {
        //let ep_addr = ep.index();

        if ep >= NUM_ENDPOINTS {
            log::error!("attempt to access out-of-bounds endpoint {:?}", ep);
            return Err(UsbError::InvalidEndpoint);
        }

        Ok(&mut self.endpoint[ep])
    }

    /// Processes an unlink and removes the pending packet.
    ///
    /// # Returns
    /// - `true` if pending urb was removed
    /// - `false` if it was not found
    fn unlink(&mut self, seqnum: u32) -> bool {
        for i in 0..NUM_ENDPOINTS {
            if self.endpoint[i].unlink(seqnum) {
                return true;
            }
        }

        false
    }
}

#[derive(Debug, Clone)]
/// An implementation of [`UsbBus`](https://docs.rs/usb-device/0.2.7/usb_device/bus/trait.UsbBus.html),
/// based on the Linux USBIP protocol.
pub struct UsbIpBus(Arc<Mutex<UsbIpBusInner>>);

impl UsbIpBus {
    /// Create a new [`UsbIpBus`].
    ///
    /// # Note
    /// There can only ever be one bus [`UsbIpBus`] device on the system, since
    /// it is blocking port 3240.
    ///
    /// # Panics
    /// If port 3240 is already in use.
    pub fn new() -> Self {
        Self(Arc::new(Mutex::new(UsbIpBusInner::new())))
    }

    fn lock(&self) -> MutexGuard<UsbIpBusInner> {
        self.0.lock().unwrap()
    }
}

impl Default for UsbIpBus {
    fn default() -> Self {
        Self::new()
    }
}

impl UsbBus for UsbIpBus {
    fn alloc_ep(
        &mut self,
        ep_dir: UsbDirection,
        ep_addr: Option<EndpointAddress>,
        ep_type: EndpointType,
        max_packet_size: u16,
        interval: u8,
    ) -> UsbResult<EndpointAddress> {
        let mut inner = self.lock();

        // Get the endpoint to initialize
        let endpoint_index = match ep_addr {
            Some(addr) => {
                if addr.index() < NUM_ENDPOINTS {
                    addr.index()
                } else {
                    return Err(UsbError::InvalidEndpoint);
                }
            }
            None => inner
                .next_available_endpoint(ep_dir)
                .ok_or(UsbError::EndpointMemoryOverflow)?,
        };

        let endpoint = &mut inner.endpoint[endpoint_index as usize];

        // check endpoint allocation here
        let maybe_pipe = match ep_dir {
            UsbDirection::In => endpoint.get_in(),
            UsbDirection::Out => endpoint.get_out(),
        };

        // we want to get an invalid enpoint, otherwise the requested endpoint
        // was already allocated
        match maybe_pipe {
            Err(UsbError::InvalidEndpoint) => (),
            Ok(_) => return Err(UsbError::InvalidEndpoint),
            Err(_) => return Err(UsbError::InvalidEndpoint),
        }

        // initialize the endpoint
        let pipe = Pipe {
            data: VecDeque::new(),
            ty: ep_type,
            max_packet_size,
            interval,
        };
        match ep_dir {
            UsbDirection::In => endpoint.pipe_in = Some(pipe),
            UsbDirection::Out => endpoint.pipe_out = Some(pipe),
        }

        log::debug!(
            "initialized new endpoint {:?} as address {:?}",
            endpoint,
            endpoint_index
        );

        Ok(EndpointAddress::from_parts(endpoint_index as usize, ep_dir))
    }

    fn enable(&mut self) {
        log::info!("usb device is being enabled");
    }

    fn reset(&self) {
        let mut inner = self.lock();

        // Skip if we are already in reset state
        if inner.reset {
            return;
        }

        inner.reset();
        log::debug!("usb device is being reset");
    }

    fn set_device_address(&self, addr: u8) {
        let mut inner = self.lock();

        log::info!("setting device address to {}", addr);
        inner.device_address = addr;
    }

    fn write(&self, ep_addr: EndpointAddress, buf: &[u8]) -> UsbResult<usize> {
        log::trace!("write request at endpoint {}", ep_addr.index());
        let mut inner = self.lock();

        // We can not write anything, as long as there is no connection
        if !inner.handler.is_connected() {
            return Err(UsbError::WouldBlock);
        }

        // Get the endpoint
        let ep = inner.get_endpoint(ep_addr.index())?;

        // NOTE: This is a hack to allow driving the setup packets in a transactional way
        // while the rest of the packets use packet logic
        if ep_addr.index() == 0 {
            ep.in_complete_flag = true;
        }

        let pipe = ep.get_in()?;

        // If there is data waiting in the output buffer, we need to wait
        if pipe.is_rts() {
            if ep_addr.index() == 0 {
                ep.in_complete_flag = false;
            }
            return Err(UsbError::WouldBlock);
        }

        pipe.data.push_back(buf.to_vec());

        // we attempt to service in packets, if we have them available
        if pipe.is_rts() {
            inner.try_send_pending(ep_addr.index());
        }

        Ok(buf.len())
    }

    fn read(&self, ep_addr: EndpointAddress, buf: &mut [u8]) -> UsbResult<usize> {
        log::trace!("read request at endpoint {}", ep_addr.index());
        let mut inner = self.lock();
        let ep = inner.get_endpoint(ep_addr.index())?;
        let pipe = ep.get_out()?;

        // Try to get data
        let data = match pipe.data.pop_front() {
            None => {
                log::trace!("no data available at endpoint");
                return Err(UsbError::WouldBlock);
            }
            Some(data) => data,
        };

        if buf.len() < data.len() {
            buf.copy_from_slice(&data[..buf.len()]);
        } else {
            buf[..data.len()].copy_from_slice(&data);
        }
        Ok(data.len())
    }

    fn set_stalled(&self, ep_addr: EndpointAddress, stalled: bool) {
        let mut inner = self.lock();

        let endpoint = match inner.get_endpoint(ep_addr.index()) {
            Ok(endpoint) => endpoint,
            _ => return,
        };

        if endpoint.stalled != stalled {
            log::debug!(
                "setting endpoint {:?} to stalled state {}",
                ep_addr,
                stalled
            );
        }
        endpoint.stalled = stalled;
    }

    fn is_stalled(&self, ep_addr: EndpointAddress) -> bool {
        let mut inner = self.lock();

        let endpoint = match inner.get_endpoint(ep_addr.index()) {
            Ok(endpoint) => endpoint,
            _ => return false,
        };

        endpoint.stalled
    }

    fn suspend(&self) {
        let mut inner = self.lock();

        log::info!("suspending device");
        if inner.suspended {
            log::warn!("supending already suspended device");
        }

        inner.suspended = true;
    }

    fn resume(&self) {
        let mut inner = self.lock();

        log::info!("resuming device");
        if !inner.suspended {
            log::warn!("resuming already active device");
        }

        inner.suspended = false;
    }

    fn poll(&self) -> PollResult {
        let mut inner = self.lock();
        log::trace!("usb device is being polled");

        inner.handle_socket();

        if inner.reset {
            log::trace!("device is in reset state");
            return PollResult::Reset;
        }

        if inner.suspended {
            log::trace!("device is suspended");
            return PollResult::Suspend;
        }

        let mut ep_in: u16 = 0;
        let mut ep_out: u16 = 0;
        let mut ep_setup: u16 = 0;

        for i in (0..NUM_ENDPOINTS).into_iter().rev() {
            ep_in <<= 1;
            ep_out <<= 1;
            ep_setup <<= 1;

            let ep = &mut inner.endpoint[i];

            // Check for pending output
            if let Some(ref pipe) = ep.pipe_out {
                if !pipe.data.is_empty() {
                    ep_out |= 1;
                }
            }

            if ep.in_complete_flag {
                ep.in_complete_flag = false;
                ep_in |= 1;
            }

            if ep.setup_flag {
                ep.setup_flag = false;
                ep_setup |= 1;
            }
        }

        PollResult::Data {
            ep_out,
            ep_in_complete: ep_in,
            ep_setup,
        }
    }
}