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
use crate::endpoint::*;
use std::fmt;
use std::io;
use std::io::Write;
use std::io::{Error, ErrorKind};

pub(crate) type Deallocate = Box<dyn Fn(*mut u8, usize) + 'static>;

pub struct ControlTransfer {
    pub buffer: *mut u8,
    // buffer length is the value sent to kernel
    pub(crate) buffer_length: u16,
    // capacity of the buffer needed when call deallocator
    pub(crate) buffer_capacity: u16,
    // given back from kernel
    pub(crate) actual_length: u16,
    deallocate: Deallocate,
}

impl fmt::Display for ControlTransfer {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "|Control|{}|{}|{}|",
            self.buffer_length, self.actual_length, self.buffer_capacity
        )
    }
}

impl ControlTransfer {
    pub(crate) fn new<DEALOC>(
        buffer: *mut u8,
        buffer_capacity: u16,
        buffer_length: u16,
        deallocate: DEALOC,
    ) -> Self
    where
        DEALOC: Fn(*mut u8, usize) + 'static,
    {
        Self {
            buffer,
            buffer_length,
            buffer_capacity,
            actual_length: 0,
            deallocate: Box::new(deallocate),
        }
    }
}

impl Drop for ControlTransfer {
    fn drop(&mut self) {
        if !self.buffer.is_null() {
            (self.deallocate)(self.buffer, self.buffer_capacity as usize);
        }
    }
}

impl Write for ControlTransfer {
    fn write(&mut self, inbuf: &[u8]) -> io::Result<usize> {
        let buf = unsafe {
            let buf = self.buffer;
            std::slice::from_raw_parts_mut(
                buf.offset(self.buffer_length as isize),
                self.buffer_capacity as usize - self.buffer_length as usize,
            )
        };

        for (i, byte) in inbuf.iter().enumerate() {
            buf[i] = *byte;
            self.buffer_length += 1;
        }
        Ok(inbuf.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        self.actual_length = 0;
        Ok(())
    }
}

pub struct BulkTransfer {
    pub(crate) buffer: *mut u8,
    // Lower layer write or read length
    pub buffer_length: usize,
    pub actual_length: usize,
    pub status: i32,
    // allocedata size
    pub buffer_capacity: usize,
    pub endpoint: Endpoint,
    // give back allocated memory
    deallocate: Deallocate,
}

impl fmt::Display for BulkTransfer {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "|{}|{}|{}|{}|",
            self.endpoint, self.buffer_length, self.actual_length, self.buffer_capacity
        )
    }
}

impl BulkTransfer {
    /// Create new bulk input (Read)
    /// the deallocate is called when BulkTransfer goes out of scope to cleanup
    /// allocated memory
    pub fn input<DEALOC>(
        ep: u8,
        buffer: *mut u8,
        buffer_capacity: usize,
        deallocate: DEALOC,
    ) -> Self
    where
        DEALOC: Fn(*mut u8, usize) + 'static,
    {
        Self {
            buffer,
            buffer_capacity,
            buffer_length: buffer_capacity,
            actual_length: 0,
            status: 0,
            endpoint: Endpoint::bulk_in(ep),
            deallocate: Box::new(deallocate),
        }
    }

    /// Create a write
    pub fn output<DEALOC>(
        ep: u8,
        buffer: *mut u8,
        buffer_capacity: usize,
        deallocate: DEALOC,
    ) -> Self
    where
        DEALOC: Fn(*mut u8, usize) + 'static,
    {
        Self {
            buffer,
            buffer_capacity,
            buffer_length: 0, // incremented when we put data in the buffer
            actual_length: 0,
            status: 0,
            endpoint: Endpoint::bulk_out(ep),
            deallocate: Box::new(deallocate),
        }
    }
}

impl Drop for BulkTransfer {
    fn drop(&mut self) {
        if !self.buffer.is_null() {
            (self.deallocate)(self.buffer, self.buffer_capacity);
        }
    }
}

impl Write for BulkTransfer {
    fn write(&mut self, inbuf: &[u8]) -> io::Result<usize> {
        if self.endpoint.is_bulk_in() {
            return Err(Error::new(
                ErrorKind::InvalidData,
                "Can not write to an bulk in endpoint.",
            ));
        }
        let buf = unsafe {
            let buf = self.buffer;
            std::slice::from_raw_parts_mut(
                buf.add(self.buffer_length),
                self.buffer_capacity - self.buffer_length,
            )
        };
        assert!(!buf.is_empty());

        for (i, byte) in inbuf.iter().enumerate() {
            buf[i] = *byte;
            self.buffer_length += 1;
        }

        Ok(inbuf.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        self.actual_length = 0;
        if self.endpoint.is_bulk_out() {
            self.buffer_length = 0;
        }
        Ok(())
    }
}

pub enum TransferKind {
    Control(ControlTransfer),
    Bulk(BulkTransfer),
    Invalid(Endpoint),
}

impl fmt::Display for TransferKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            TransferKind::Control(control) => write!(f, "Control Transfer: {}", control),
            TransferKind::Bulk(bulk) => write!(f, "Control Transfer: {}", bulk),
            TransferKind::Invalid(ep) => write!(f, "Invalid {}", ep),
        }
    }
}

pub trait BufferSlice {
    fn buffer_from_raw<'a>(&self) -> &'a [u8];
}

pub trait UsbCoreDriver {
    fn new_bulk_in(&mut self, ep: u8, read_capacity: usize) -> io::Result<BulkTransfer>;
    fn new_bulk_out(&mut self, ep: u8, capacity: usize) -> io::Result<BulkTransfer>;

    // Create a new control
    fn new_control(
        &mut self,
        request_type: u8, // bRequestType
        request: u8,      // bRequest
        value: u16,       // wValue
        index: u16,       // wIndex
        length: u16,      // wLength
    ) -> io::Result<ControlTransfer>;

    fn new_control_out(
        &mut self,
        request_type: u8,
        request: u8,
        value: u16,
        index: u16,
        buffer: &[u8],
    ) -> io::Result<ControlTransfer> {
        let mut ctrl = self.new_control(
            request_type | ENDPOINT_OUT,
            request,
            value,
            index,
            buffer.len() as u16, // wLength
        )?;
        ctrl.write_all(buffer)?;
        Ok(ctrl)
    }

    fn new_control_in(
        &mut self,
        request_type: u8,
        request: u8,
        value: u16,
        index: u16,
        length: u16, // wLength (Read length)
    ) -> io::Result<ControlTransfer> {
        let mut ctrl =
            self.new_control(request_type | ENDPOINT_IN, request, value, index, length)?;
        ctrl.buffer_length += length;
        Ok(ctrl)
    }

    // Create a new control
    fn new_control_nodata(
        &mut self,
        request_type: u8,
        request: u8,
        value: u16,
        index: u16,
    ) -> io::Result<ControlTransfer> {
        self.new_control(request_type, request, value, index, 0)
    }
}