usbh 0.1.0

Experimental host-side USB stack for embedded devices.
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
use super::Driver;
use crate::bus::HostBus;
use crate::descriptor;
use crate::types::{ConnectionSpeed, DeviceAddress, SetupPacket, TransferType};
use crate::{ControlError, PipeId, UsbHost};
use core::num::NonZeroU8;
use usb_device::{
    control::{Recipient, RequestType},
    UsbDirection,
};

/// Driver for boot keyboards
///
/// By default, up to 8 connected keyboards can be handled. Events are reported for
/// each device separately.
///
/// To increase (or decrease) the number of devices that can be handled, adjust the `MAX_DEVICES` parameter.
///
/// Note: the number of devices that can be handled also depends on [`UsbHost`] which limits the number of pipes that can be created.
///   Each connected keyboard requires two pipes: a control pipe and an interrupt pipe.
pub struct KbdDriver<const MAX_DEVICES: usize = 8> {
    devices: [Option<KbdDevice>; MAX_DEVICES],
    event: Option<KbdEvent>,
}

#[derive(Copy, Clone)]
struct KbdDevice {
    device_address: DeviceAddress,
    inner: KbdDeviceInner,
}

#[derive(Copy, Clone)]
enum KbdDeviceInner {
    Pending(PendingKbdDevice),
    Configured(ConfiguredKbdDevice),
}

impl KbdDeviceInner {
    fn pending() -> Self {
        KbdDeviceInner::Pending(PendingKbdDevice {
            config: None,
            interface: None,
            endpoint: None,
            interval: None,
        })
    }
}

#[derive(Copy, Clone)]
struct PendingKbdDevice {
    config: Option<u8>,
    interface: Option<u8>,
    endpoint: Option<u8>,
    interval: Option<u8>,
}

#[derive(Copy, Clone)]
struct ConfiguredKbdDevice {
    interface: u8,
    control_pipe: PipeId,
    interrupt_pipe: PipeId,
    output_report: u8,
}

impl PendingKbdDevice {
    /// Returns the detected configuration value, if it is usable
    ///
    /// A configuration is ocnsidered usable, if it has:
    /// - an interface, with the correct class, subclass and protocol
    /// - an IN interrupt endpoint
    fn supported_config(&self) -> Option<u8> {
        self.interface
            .and_then(|_| self.endpoint)
            .and_then(|_| self.interval)
            .and_then(|_| self.config)
    }
}

/// Represents an input report, received from a keyboard
///
/// The input report describes which keys are currently pressed.
#[derive(Copy, Clone, defmt::Format)]
#[repr(packed)]
pub struct InputReport {
    /// Status of modifier keys
    pub modifier_status: ModifierStatus,
    _reserved: u8,

    pub keypress: [Option<NonZeroU8>; 6],
}

impl InputReport {
    pub fn pressed_keys(&self) -> impl Iterator<Item = u8> + '_ {
        self.keypress
            .iter()
            .filter_map(|opt| *opt)
            .map(|code| code.into())
    }
}

impl<'a> TryFrom<&'a [u8]> for &'a InputReport {
    type Error = ();

    fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
        if value.len() == 8 && core::mem::size_of::<InputReport>() == 8 {
            // Safety: we have verified that the InputReport struct and the provided value have the expected size
            Ok(unsafe { &*(value as *const _ as *const InputReport) })
        } else {
            Err(())
        }
    }
}

#[derive(Debug, Copy, Clone, defmt::Format)]
pub struct ModifierStatus(u8);

impl ModifierStatus {
    /// Is left `Ctrl` pressed?
    pub fn left_ctrl(&self) -> bool {
        self.0 & 1 == 1
    }

    /// Is left `Shift` pressed?
    pub fn left_shift(&self) -> bool {
        (self.0 >> 1) & 1 == 1
    }

    /// Is left `Alt` pressed?
    pub fn left_alt(&self) -> bool {
        (self.0 >> 2) & 1 == 1
    }

    /// Is left `Gui` pressed?
    ///
    /// The `Gui` button is also known as the `Super` or `Windows` key.
    pub fn left_gui(&self) -> bool {
        (self.0 >> 3) & 1 == 1
    }

    /// Is right `Ctrl` pressed?
    pub fn right_ctrl(&self) -> bool {
        (self.0 >> 4) & 1 == 1
    }

    /// Is right `Shift` pressed?
    pub fn right_shift(&self) -> bool {
        (self.0 >> 5) & 1 == 1
    }

    /// Is right `Alt` pressed?
    pub fn right_alt(&self) -> bool {
        (self.0 >> 6) & 1 == 1
    }

    /// Is right `Gui` pressed?
    ///
    /// The `Gui` button is also known as the `Super` or `Windows` key.
    pub fn right_gui(&self) -> bool {
        (self.0 >> 7) & 1 == 1
    }
}

/// Events related to attached keyboard(s)
#[derive(Copy, Clone, defmt::Format)]
pub enum KbdEvent {
    /// A new keyboard was detected & configured, with given device address
    DeviceAdded(DeviceAddress),

    /// A keyboard was removed
    DeviceRemoved(DeviceAddress),

    /// The input report changed for one of the devices.
    ///
    /// Use the [`InputReport`] object to find out more.
    InputChanged(DeviceAddress, InputReport),

    /// A control transfer has completed.
    ///
    /// Control transfers are initiated by the [`KbdDriver::set_idle`] and [`KbdDriver::set_led`] methods.
    ControlComplete(DeviceAddress),
}

/// Identifies the five LEDs that a boot keyboard can support
#[derive(Copy, Clone)]
#[repr(u8)]
pub enum KbdLed {
    NumLock = 0,
    CapsLock = 1,
    ScrollLock = 2,
    Compose = 3,
    Kana = 4,
}

/// Error type for interactions with the driver
#[derive(Copy, Clone)]
pub enum KbdError {
    /// Error initiating control transfer
    ControlError(ControlError),

    /// The given `DeviceAddress` is not known.
    ///
    /// This can happen if the device was removed meanwhile.
    UnknownDevice,
}

impl From<ControlError> for KbdError {
    fn from(e: ControlError) -> Self {
        KbdError::ControlError(e)
    }
}

impl<const MAX_DEVICES: usize> KbdDriver<MAX_DEVICES> {
    pub fn new() -> Self {
        Self {
            devices: [None; MAX_DEVICES],
            event: None,
        }
    }

    /// Returns the last keyboard event that occurred (if any) and clears it.
    ///
    /// This method should be called directly after calling `usb_host.poll(...)`.
    ///
    /// Otherwise events may be lost.
    ///
    /// For the meaning of events, please refer to the [`KbdEvent`] documentation.
    pub fn take_event(&mut self) -> Option<KbdEvent> {
        self.event.take()
    }

    /// Set interval for idle reports
    ///
    /// If an idle interval is set, the keyboard will send out the current input report (i.e. pressed keys)
    /// regularly, even when no change to the pressed keys occurs.
    ///
    /// The interval is expressed as a `duration` value, which is interpreted as a *multiple of 4 ms*.
    ///
    /// Setting the duration to `0` disables idle reports. If they are disabled, input reports are only
    /// received when a key is pressed or released.
    ///
    /// The USB HID specification recommends a default interval of 500ms for keyboards (duration value: 125).
    ///
    pub fn set_idle<B: HostBus>(
        &mut self,
        dev_addr: DeviceAddress,
        latency: u8,
        host: &mut UsbHost<B>,
    ) -> Result<(), KbdError> {
        if let Some(device) = self.find_configured_device(dev_addr) {
            host.control_out(
                Some(dev_addr),
                Some(device.control_pipe),
                SetupPacket::new(
                    UsbDirection::Out,
                    RequestType::Class,
                    Recipient::Interface,
                    0x0a, // SetIdle
                    (latency as u16) << 8,
                    device.interface as u16,
                    0,
                ),
                &[],
            )?;
            Ok(())
        } else {
            Err(KbdError::UnknownDevice)
        }
    }

    /// Set the given [`KbdLed`] to the specified state.
    ///
    /// The driver keeps track of the current output report (i.e. LED state basically) for each of the connected
    /// devices. Initially it is 0 (i.e. all LEDs are off).
    ///
    /// This method updates one of the bits in the output report (identified by [`KbdLed`]) and sents the
    /// updated report to the device.
    pub fn set_led<B: HostBus>(
        &mut self,
        dev_addr: DeviceAddress,
        led: KbdLed,
        on: bool,
        host: &mut UsbHost<B>,
    ) -> Result<(), KbdError> {
        if let Some(device) = self.find_configured_device(dev_addr) {
            if on {
                device.output_report |= 1 << (led as u8);
            } else {
                device.output_report &= !(1 << (led as u8));
            }
            host.control_out(
                Some(dev_addr),
                Some(device.control_pipe),
                SetupPacket::new(
                    UsbDirection::Out,
                    RequestType::Class,
                    Recipient::Interface,
                    0x09,   // SetReport,
                    2 << 8, // 2 means "output" report
                    0,
                    1,
                ),
                &[device.output_report],
            )?;
            Ok(())
        } else {
            Err(KbdError::UnknownDevice)
        }
    }

    fn find_device_slot(
        &mut self,
        device_address: DeviceAddress,
    ) -> Option<&mut Option<KbdDevice>> {
        self.devices.iter_mut().find(|dev| {
            if let Some(dev) = dev {
                dev.device_address == device_address
            } else {
                false
            }
        })
    }

    fn find_device(&mut self, device_address: DeviceAddress) -> Option<&mut KbdDevice> {
        if let Some(Some(device)) = self.find_device_slot(device_address) {
            Some(device)
        } else {
            None
        }
    }

    fn find_pending_device(
        &mut self,
        device_address: DeviceAddress,
    ) -> Option<&mut PendingKbdDevice> {
        match self.find_device(device_address) {
            Some(KbdDevice {
                inner: KbdDeviceInner::Pending(pending_device),
                ..
            }) => Some(pending_device),
            _ => None,
        }
    }

    fn find_configured_device(
        &mut self,
        device_address: DeviceAddress,
    ) -> Option<&mut ConfiguredKbdDevice> {
        match self.find_device(device_address) {
            Some(KbdDevice {
                inner: KbdDeviceInner::Configured(device),
                ..
            }) => Some(device),
            _ => None,
        }
    }

    fn remove_device(&mut self, device_address: DeviceAddress) {
        if let Some(slot) = self.find_device_slot(device_address) {
            slot.take();
        }
    }
}

impl<B: HostBus> Driver<B> for KbdDriver {
    fn attached(&mut self, device_address: DeviceAddress, _connection_speed: ConnectionSpeed) {
        if let Some(slot) = self.devices.iter_mut().find(|dev| dev.is_none()) {
            slot.replace(KbdDevice {
                device_address,
                inner: KbdDeviceInner::pending(),
            });
        } else {
            // maximum number of devices reached.
        }
    }

    fn detached(&mut self, device_address: DeviceAddress) {
        if let Some(slot) = self.find_device_slot(device_address) {
            if let Some(KbdDevice {
                inner: KbdDeviceInner::Configured(_),
                ..
            }) = slot.take()
            {
                self.event = Some(KbdEvent::DeviceRemoved(device_address));
            }
        }
    }

    fn descriptor(&mut self, device_address: DeviceAddress, descriptor_type: u8, data: &[u8]) {
        if let Some(device) = self.find_pending_device(device_address) {
            if descriptor_type == descriptor::TYPE_CONFIGURATION as u8 {
                if device.interface.is_none() {
                    // we only care about new configurations if we haven't already found an interface that we can handle
                    if let Ok((_, config)) = descriptor::parse::configuration_descriptor(data) {
                        // keep track of the config value. If we encounter an interface descriptor within this configuration that
                        // we can handle, this will remain the final value.
                        // Otherwise the next config descriptor will overwrite it.
                        device.config = Some(config.value);
                    }
                }
            } else if descriptor_type == descriptor::TYPE_INTERFACE {
                if let Ok((_, interface)) = descriptor::parse::interface_descriptor(data) {
                    if interface.interface_class == 0x03 && // HID
                        interface.interface_sub_class == 0x01 && // boot interface
                        interface.interface_protocol  == 0x01
                    {
                        // keyboard
                        device.interface = Some(interface.interface_number);
                    }
                }
            } else if descriptor_type == descriptor::TYPE_ENDPOINT {
                if device.interface.is_some() && device.endpoint.is_none() {
                    if let Ok((_, endpoint)) = descriptor::parse::endpoint_descriptor(data) {
                        if endpoint.address.direction() == UsbDirection::In
                            && endpoint.attributes.transfer_type() == TransferType::Interrupt
                        {
                            device.endpoint = Some(endpoint.address.number());
                            device.interval = Some(endpoint.interval);
                        }
                    }
                }
            }
        }
    }

    fn configure(&mut self, device_address: DeviceAddress) -> Option<u8> {
        // We choose a configuration only if we found an interface that we can handle
        let config = self
            .find_pending_device(device_address)
            .and_then(|device| device.supported_config());

        if config.is_none() {
            // clean up this device. We cannot handle it.
            self.remove_device(device_address);
        }

        config
    }

    fn configured(&mut self, device_address: DeviceAddress, value: u8, host: &mut UsbHost<B>) {
        let configured_device = if let Some(device) = self.find_pending_device(device_address) {
            if let Some(config) = device.supported_config() {
                if value != config {
                    // a different configuration was selected for this device. We can't handle it (probably).
                    None
                } else {
                    // Unwrap safety: supported_config() verifies there is a value
                    let interface = device.interface.unwrap();
                    let control_pipe = host.create_control_pipe(device_address);
                    let interrupt_pipe = host.create_interrupt_pipe(
                        device_address,
                        // Unwrap safety: supported_config() verifies there is a value
                        device.endpoint.unwrap(),
                        UsbDirection::In,
                        8,
                        // Unwrap safety: supported_config() verifies there is a value
                        device.interval.unwrap(),
                    );
                    self.event = Some(KbdEvent::DeviceAdded(device_address));
                    match (control_pipe, interrupt_pipe) {
                        (Some(control_pipe), Some(interrupt_pipe)) => Some(ConfiguredKbdDevice {
                            interface,
                            control_pipe,
                            interrupt_pipe,
                            output_report: 0,
                        }),
                        _ => None,
                    }
                }
            } else {
                // no supported configuration was found for the device
                None
            }
        } else {
            // we don't know this device (max devices reached, or already removed)
            None
        };

        if let Some(configured_device) = configured_device {
            // Unwrap safety: if `find_pending_device` above succeeded, then `find_device_slot` will succeed here as well
            self.find_device_slot(device_address)
                .unwrap()
                .replace(KbdDevice {
                    device_address,
                    inner: KbdDeviceInner::Configured(configured_device),
                });
        } else {
            self.remove_device(device_address);
        }
    }

    fn completed_control(
        &mut self,
        dev_addr: DeviceAddress,
        _pipe_id: PipeId,
        _data: Option<&[u8]>,
    ) {
        self.event = Some(KbdEvent::ControlComplete(dev_addr));
    }

    fn completed_in(&mut self, device_address: DeviceAddress, pipe: PipeId, data: &[u8]) {
        if let Some(device) = self.find_configured_device(device_address) {
            if pipe == device.interrupt_pipe {
                let converted: Result<&InputReport, _> = data.try_into();
                if let Ok(input_report) = converted {
                    self.event = Some(KbdEvent::InputChanged(device_address, *input_report));
                }
            }
        }
    }

    fn completed_out(
        &mut self,
        _device_address: DeviceAddress,
        _pipe_id: PipeId,
        _data: &mut [u8],
    ) {
        // ignored, since there are no OUT pipes in use.
    }
}