aboutsummaryrefslogtreecommitdiff
path: root/src/hid.rs
blob: 9de68d64c60174ac1248d6664a9f1ba3321b2dc7 (plain)
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
// Copyright 2019 Robin Krahl <robin.krahl@ireas.org>
// SPDX-License-Identifier: GPL-3.0-or-later

use usb_device::bus::{InterfaceNumber, StringIndex, UsbBus, UsbBusAllocator};
use usb_device::class::{ControlIn, ControlOut, UsbClass};
use usb_device::descriptor::DescriptorWriter;
use usb_device::endpoint::{EndpointAddress, EndpointIn};
use usb_device::Result;

const INTERFACE_CLASS_HID: u8 = 0x03;

enum_u8! {
    #[derive(Clone, Copy, Debug, PartialEq)]
    pub enum Subclass {
        None = 0x00,
        BootInterface = 0x01,
    }
}

enum_u8! {
    #[derive(Clone, Copy, Debug, PartialEq)]
    pub enum Protocol {
        None = 0x00,
        Keyboard = 0x01,
        Mouse = 0x02,
    }
}

pub struct HidClass<'a, B: UsbBus> {
    interface: InterfaceNumber,
    endpoint_interrupt_in: EndpointIn<'a, B>,
    expect_interrupt_in_complete: bool,
    subclass: Subclass,
    protocol: Protocol,
}

impl<B: UsbBus> HidClass<'_, B> {
    pub fn new(
        alloc: &UsbBusAllocator<B>,
        subclass: Subclass,
        protocol: Protocol,
    ) -> HidClass<'_, B> {
        HidClass {
            interface: alloc.interface(),
            endpoint_interrupt_in: alloc.interrupt(8, 10),
            expect_interrupt_in_complete: false,
            subclass,
            protocol,
        }
    }
}

impl<B: UsbBus> UsbClass<B> for HidClass<'_, B> {
    fn poll(&mut self) {}

    fn reset(&mut self) {
        self.expect_interrupt_in_complete = false;
    }

    fn get_configuration_descriptors(&self, writer: &mut DescriptorWriter) -> Result<()> {
        writer.interface(
            self.interface,
            INTERFACE_CLASS_HID,
            self.subclass.into(),
            self.protocol.into(),
        )?;
        writer.endpoint(&self.endpoint_interrupt_in)?;

        Ok(())
    }

    fn get_string(&self, _index: StringIndex, _lang_id: u16) -> Option<&str> {
        None
    }

    fn endpoint_in_complete(&mut self, addr: EndpointAddress) {
        if addr == self.endpoint_interrupt_in.address() {
            if self.expect_interrupt_in_complete {
                self.expect_interrupt_in_complete = false;
            } else {
                panic!("unexpected endpoint_in_complete");
            }
        }
    }

    fn endpoint_out(&mut self, _addr: EndpointAddress) {}

    fn control_in(&mut self, _xfer: ControlIn<B>) {}

    fn control_out(&mut self, _xfer: ControlOut<B>) {}
}