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
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2023.

//! Keyboard USB HID device

use super::descriptors;
use super::descriptors::Buffer64;
use super::descriptors::DescriptorType;
use super::descriptors::EndpointAddress;
use super::descriptors::EndpointDescriptor;
use super::descriptors::HIDCountryCode;
use super::descriptors::HIDDescriptor;
use super::descriptors::HIDSubordinateDescriptor;
use super::descriptors::InterfaceDescriptor;
use super::descriptors::ReportDescriptor;
use super::descriptors::TransferDirection;
use super::usbc_client_ctrl::ClientCtrl;

use kernel::hil;
use kernel::hil::usb::TransferType;
use kernel::utilities::cells::OptionalCell;
use kernel::utilities::cells::TakeCell;
use kernel::ErrorCode;

/// Use 1 Interrupt transfer IN/OUT endpoint
const ENDPOINT_NUM: usize = 1;

const IN_BUFFER: usize = 0;

static LANGUAGES: &[u16; 1] = &[
    0x0409, // English (United States)
];
/// Max packet size specified by spec
pub const MAX_CTRL_PACKET_SIZE: u8 = 64;

const N_ENDPOINTS: usize = 1;

/// The HID report descriptor for keyboard from
/// <https://www.usb.org/sites/default/files/hid1_11.pdf>.
static REPORT_DESCRIPTOR: &[u8] = &[
    0x05, 0x01, // Usage Page (Generic Desktop),
    0x09, 0x06, // Usage (Keyboard),
    0xA1, 0x01, // Collection (Application),
    0x75, 0x01, // Report Size (1),
    0x95, 0x08, // Report Count (8),
    0x05, 0x07, // Usage Page (Key Codes),
    0x19, 0xE0, // Usage Minimum (224),
    0x29, 0xE7, // Usage Maximum (231),
    0x15, 0x00, // Logical Minimum (0),
    0x25, 0x01, // Logical Maximum (1),
    0x81, 0x02, // Input (Data, Variable, Absolute),
    // ;Modifier byte
    0x95, 0x01, // Report Count (1),
    0x75, 0x08, // Report Size (8),
    0x81, 0x03, // Input (Constant),
    // ;Reserved byte
    0x95, 0x05, // Report Count (5),
    0x75, 0x01, // Report Size (1),
    0x05, 0x08, // Usage Page (LEDs),
    0x19, 0x01, // Usage Minimum (1),
    0x29, 0x05, // Usage Maximum (5),
    0x91, 0x02, // Output (Data, Variable, Absolute),
    // ;LED report
    0x95, 0x01, // Report Count (1),
    0x75, 0x03, // Report Size (3),
    0x91, 0x03, // Output (Constant), ;LED report
    // padding
    0x95, 0x06, //................// Report Count (6),
    0x75, 0x08, // Report Size (8),
    0x15, 0x00, // Logical Minimum (0),
    0x25, 0x68, // Logical Maximum(104),
    0x05, 0x07, // Usage Page (Key Codes),
    0x19, 0x00, // Usage Minimum (0),
    0x29, 0x68, // Usage Maximum (104),
    0x81, 0x00, // Input (Data, Array),
    0xc0, // End Collection
];

static REPORT: ReportDescriptor<'static> = ReportDescriptor {
    desc: REPORT_DESCRIPTOR,
};

static SUB_HID_DESCRIPTOR: &[HIDSubordinateDescriptor] = &[HIDSubordinateDescriptor {
    typ: DescriptorType::Report,
    len: REPORT_DESCRIPTOR.len() as u16,
}];

static HID_DESCRIPTOR: HIDDescriptor<'static> = HIDDescriptor {
    hid_class: 0x0111,
    country_code: HIDCountryCode::NotSupported,
    sub_descriptors: SUB_HID_DESCRIPTOR,
};

/// Implementation of the CTAP HID (Human Interface Device)
pub struct KeyboardHid<'a, U: 'a> {
    /// Helper USB client library for handling many USB operations.
    client_ctrl: ClientCtrl<'a, 'static, U>,

    /// 64 byte buffers for each endpoint.
    buffers: [Buffer64; N_ENDPOINTS],

    client: OptionalCell<&'a dyn hil::usb_hid::Client<'a, [u8; 64]>>,

    /// A buffer to hold the data we want to send
    send_buffer: TakeCell<'static, [u8; 64]>,
}

impl<'a, U: hil::usb::UsbController<'a>> KeyboardHid<'a, U> {
    pub fn new(
        controller: &'a U,
        vendor_id: u16,
        product_id: u16,
        strings: &'static [&'static str; 3],
    ) -> Self {
        let interfaces: &mut [InterfaceDescriptor] = &mut [InterfaceDescriptor {
            interface_number: 0,
            interface_class: 0x03,    // HID
            interface_subclass: 0x01, // Boot subclass
            interface_protocol: 0x01, // Keyboard
            ..InterfaceDescriptor::default()
        }];

        let endpoints: &[&[EndpointDescriptor]] = &[&[EndpointDescriptor {
            endpoint_address: EndpointAddress::new_const(
                ENDPOINT_NUM,
                TransferDirection::DeviceToHost,
            ),
            transfer_type: TransferType::Interrupt,
            max_packet_size: 8,
            interval: 10,
        }]];

        let (device_descriptor_buffer, other_descriptor_buffer) =
            descriptors::create_descriptor_buffers(
                descriptors::DeviceDescriptor {
                    vendor_id,
                    product_id,
                    manufacturer_string: 1,
                    product_string: 2,
                    serial_number_string: 3,
                    max_packet_size_ep0: MAX_CTRL_PACKET_SIZE,
                    ..descriptors::DeviceDescriptor::default()
                },
                descriptors::ConfigurationDescriptor {
                    attributes: descriptors::ConfigurationAttributes::new(true, true),
                    max_power: 0x32,
                    ..descriptors::ConfigurationDescriptor::default()
                },
                interfaces,
                endpoints,
                Some(&HID_DESCRIPTOR),
                None,
            );

        KeyboardHid {
            client_ctrl: ClientCtrl::new(
                controller,
                device_descriptor_buffer,
                other_descriptor_buffer,
                Some(&HID_DESCRIPTOR),
                Some(&REPORT),
                LANGUAGES,
                strings,
            ),
            buffers: [Buffer64::default()],
            client: OptionalCell::empty(),
            send_buffer: TakeCell::empty(),
        }
    }

    #[inline]
    fn controller(&self) -> &'a U {
        self.client_ctrl.controller()
    }

    pub fn set_client(&'a self, client: &'a dyn hil::usb_hid::Client<'a, [u8; 64]>) {
        self.client.set(client);
    }
}

impl<'a, U: hil::usb::UsbController<'a>> hil::usb_hid::UsbHid<'a, [u8; 64]> for KeyboardHid<'a, U> {
    fn send_buffer(
        &'a self,
        send: &'static mut [u8; 64],
    ) -> Result<usize, (ErrorCode, &'static mut [u8; 64])> {
        let len = send.len();

        self.send_buffer.replace(send);
        self.controller().endpoint_resume_in(ENDPOINT_NUM);

        Ok(len)
    }

    fn send_cancel(&'a self) -> Result<&'static mut [u8; 64], ErrorCode> {
        match self.send_buffer.take() {
            Some(buf) => Ok(buf),
            None => Err(ErrorCode::BUSY),
        }
    }

    // Keyboard doesn't use receive so this is unimplemented.
    fn receive_buffer(
        &'a self,
        _recv: &'static mut [u8; 64],
    ) -> Result<(), (ErrorCode, &'static mut [u8; 64])> {
        Ok(())
    }

    // Keyboard doesn't use receive so this is unimplemented.
    fn receive_cancel(&'a self) -> Result<&'static mut [u8; 64], ErrorCode> {
        Err(ErrorCode::BUSY)
    }
}

impl<'a, U: hil::usb::UsbController<'a>> hil::usb::Client<'a> for KeyboardHid<'a, U> {
    fn enable(&'a self) {
        // Set up the default control endpoint
        self.client_ctrl.enable();

        // Setup buffers for IN data transfer.
        self.controller()
            .endpoint_set_in_buffer(ENDPOINT_NUM, &self.buffers[IN_BUFFER].buf);
        self.controller()
            .endpoint_in_out_enable(TransferType::Interrupt, ENDPOINT_NUM);
    }

    fn attach(&'a self) {
        self.client_ctrl.attach();
    }

    fn bus_reset(&'a self) {}

    /// Handle a Control Setup transaction.
    fn ctrl_setup(&'a self, endpoint: usize) -> hil::usb::CtrlSetupResult {
        self.client_ctrl.ctrl_setup(endpoint)
    }

    /// Handle a Control In transaction
    fn ctrl_in(&'a self, endpoint: usize) -> hil::usb::CtrlInResult {
        self.client_ctrl.ctrl_in(endpoint)
    }

    /// Handle a Control Out transaction
    fn ctrl_out(&'a self, _endpoint: usize, _packet_bytes: u32) -> hil::usb::CtrlOutResult {
        // self.client_ctrl.ctrl_out(endpoint, packet_bytes)
        hil::usb::CtrlOutResult::Ok
    }

    fn ctrl_status(&'a self, endpoint: usize) {
        self.client_ctrl.ctrl_status(endpoint)
    }

    /// Handle the completion of a Control transfer
    fn ctrl_status_complete(&'a self, endpoint: usize) {
        self.client_ctrl.ctrl_status_complete(endpoint)
    }

    /// Handle a Bulk/Interrupt IN transaction.
    ///
    /// This is called when we can send data to the host. It should get called
    /// when we tell the controller we want to resume the IN endpoint (meaning
    /// we know we have data to send) and afterwards until we return
    /// `hil::usb::InResult::Delay` from this function. That means we can use
    /// this as a callback to mean that the transmission finished by waiting
    /// until this function is called when we don't have anything left to send.
    fn packet_in(&'a self, transfer_type: TransferType, _endpoint: usize) -> hil::usb::InResult {
        match transfer_type {
            TransferType::Interrupt => {
                self.send_buffer
                    .take()
                    .map_or(hil::usb::InResult::Delay, |buf| {
                        // Get packet that we have shared with the underlying
                        // USB stack to copy the tx into.
                        let packet = &self.buffers[IN_BUFFER].buf;

                        // Copy from the TX buffer to the outgoing USB packet.
                        // Our endpoint supports exactly 8 bytes so we use that
                        // length.
                        for i in 0..8 {
                            packet[i].set(buf[i]);
                        }

                        // Put the TX buffer back so we can keep sending from
                        // it.
                        self.send_buffer.replace(buf);

                        // Return that we have data to send.
                        hil::usb::InResult::Packet(8)
                    })
            }
            TransferType::Bulk | TransferType::Control | TransferType::Isochronous => {
                hil::usb::InResult::Error
            }
        }
    }

    /// Handle a Bulk/Interrupt OUT transaction
    ///
    /// Unused for keyboard.
    fn packet_out(
        &'a self,
        transfer_type: TransferType,
        _endpoint: usize,
        _packet_bytes: u32,
    ) -> hil::usb::OutResult {
        match transfer_type {
            TransferType::Interrupt => hil::usb::OutResult::Ok,

            TransferType::Bulk | TransferType::Control | TransferType::Isochronous => {
                hil::usb::OutResult::Error
            }
        }
    }

    fn packet_transmitted(&'a self, endpoint: usize) {
        self.send_buffer.take().map(|buf| {
            self.client.map(move |client| {
                client.packet_transmitted(Ok(()), buf, endpoint);
            });
        });
    }
}