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

//! Provides userspace with access to USB HID devices with a simple syscall
//! interface.

use core::cell::Cell;
use core::marker::PhantomData;

use kernel::grant::{AllowRoCount, AllowRwCount, Grant, UpcallCount};
use kernel::hil::usb_hid;
use kernel::processbuffer::{ReadableProcessBuffer, WriteableProcessBuffer};
use kernel::syscall::{CommandReturn, SyscallDriver};
use kernel::utilities::cells::{OptionalCell, TakeCell};
use kernel::{ErrorCode, ProcessId};

/// Ids for read-write allow buffers
mod rw_allow {
    pub const RECV: usize = 0;
    pub const SEND: usize = 1;
    /// The number of allow buffers the kernel stores for this grant
    pub const COUNT: u8 = 2;
}

pub struct App {
    can_receive: Cell<bool>,
}

impl Default for App {
    fn default() -> App {
        App {
            can_receive: Cell::new(false),
        }
    }
}

pub struct UsbHidDriver<'a, U: usb_hid::UsbHid<'a, [u8; 64]>> {
    usb: Option<&'a U>,

    app: Grant<App, UpcallCount<1>, AllowRoCount<0>, AllowRwCount<{ rw_allow::COUNT }>>,
    processid: OptionalCell<ProcessId>,
    phantom: PhantomData<&'a U>,

    send_buffer: TakeCell<'static, [u8; 64]>,
    recv_buffer: TakeCell<'static, [u8; 64]>,
}

impl<'a, U: usb_hid::UsbHid<'a, [u8; 64]>> UsbHidDriver<'a, U> {
    pub fn new(
        usb: Option<&'a U>,
        send_buffer: &'static mut [u8; 64],
        recv_buffer: &'static mut [u8; 64],
        grant: Grant<App, UpcallCount<1>, AllowRoCount<0>, AllowRwCount<{ rw_allow::COUNT }>>,
    ) -> UsbHidDriver<'a, U> {
        UsbHidDriver {
            usb,
            app: grant,
            processid: OptionalCell::empty(),
            phantom: PhantomData,
            send_buffer: TakeCell::new(send_buffer),
            recv_buffer: TakeCell::new(recv_buffer),
        }
    }
}

impl<'a, U: usb_hid::UsbHid<'a, [u8; 64]>> usb_hid::UsbHid<'a, [u8; 64]> for UsbHidDriver<'a, U> {
    fn send_buffer(
        &'a self,
        send: &'static mut [u8; 64],
    ) -> Result<usize, (ErrorCode, &'static mut [u8; 64])> {
        if let Some(usb) = self.usb {
            usb.send_buffer(send)
        } else {
            Err((ErrorCode::NOSUPPORT, send))
        }
    }

    fn send_cancel(&'a self) -> Result<&'static mut [u8; 64], ErrorCode> {
        if let Some(usb) = self.usb {
            usb.send_cancel()
        } else {
            Err(ErrorCode::NOSUPPORT)
        }
    }

    fn receive_buffer(
        &'a self,
        recv: &'static mut [u8; 64],
    ) -> Result<(), (ErrorCode, &'static mut [u8; 64])> {
        if let Some(usb) = self.usb {
            usb.receive_buffer(recv)
        } else {
            Err((ErrorCode::NODEVICE, recv))
        }
    }

    fn receive_cancel(&'a self) -> Result<&'static mut [u8; 64], ErrorCode> {
        if let Some(usb) = self.usb {
            usb.receive_cancel()
        } else {
            Err(ErrorCode::NOSUPPORT)
        }
    }
}

impl<'a, U: usb_hid::UsbHid<'a, [u8; 64]>> usb_hid::Client<'a, [u8; 64]> for UsbHidDriver<'a, U> {
    fn packet_received(
        &'a self,
        _result: Result<(), ErrorCode>,
        buffer: &'static mut [u8; 64],
        _endpoint: usize,
    ) {
        self.processid.map(|id| {
            self.app
                .enter(id, |app, kernel_data| {
                    let _ = kernel_data
                        .get_readwrite_processbuffer(rw_allow::RECV)
                        .and_then(|recv| {
                            recv.mut_enter(|dest| {
                                dest.copy_from_slice(buffer);
                            })
                        });

                    kernel_data.schedule_upcall(0, (0, 0, 0)).ok();
                    app.can_receive.set(false);
                })
                .map_err(|err| {
                    if err == kernel::process::Error::NoSuchApp
                        || err == kernel::process::Error::InactiveApp
                    {}
                })
        });

        self.recv_buffer.replace(buffer);
    }

    fn packet_transmitted(
        &'a self,
        _result: Result<(), ErrorCode>,
        buffer: &'static mut [u8; 64],
        _endpoint: usize,
    ) {
        self.processid.map(|id| {
            self.app
                .enter(id, |_app, kernel_data| {
                    kernel_data.schedule_upcall(0, (1, 0, 0)).ok();
                })
                .map_err(|err| {
                    if err == kernel::process::Error::NoSuchApp
                        || err == kernel::process::Error::InactiveApp
                    {}
                })
        });

        // Save our send buffer so we can use it later
        self.send_buffer.replace(buffer);
    }

    fn can_receive(&'a self) -> bool {
        self.processid
            .map(|id| {
                self.app
                    .enter(id, |app, _| app.can_receive.get())
                    .unwrap_or(false)
            })
            .unwrap_or(false)
    }
}

impl<'a, U: usb_hid::UsbHid<'a, [u8; 64]>> SyscallDriver for UsbHidDriver<'a, U> {
    // Subscribe to UsbHidDriver events.
    //
    // ### `subscribe_num`
    //
    // - `0`: Subscribe to interrupts from HID events.
    //        The callback signature is `fn(direction: u32)`
    //        `fn(0)` indicates a packet was received
    //        `fn(1)` indicates a packet was transmitted

    fn command(
        &self,
        command_num: usize,
        _data1: usize,
        _data2: usize,
        processid: ProcessId,
    ) -> CommandReturn {
        let can_access = self.processid.map_or(true, |owning_app| {
            if owning_app == processid {
                // We own the HID device
                true
            } else {
                false
            }
        });

        if !can_access {
            return CommandReturn::failure(ErrorCode::BUSY);
        }

        match command_num {
            0 => CommandReturn::success(),

            // Send data
            1 => self
                .app
                .enter(processid, |_, kernel_data| {
                    self.processid.set(processid);
                    if let Some(usb) = self.usb {
                        kernel_data
                            .get_readwrite_processbuffer(rw_allow::SEND)
                            .and_then(|send| {
                                send.enter(|data| {
                                    self.send_buffer.take().map_or(
                                        CommandReturn::failure(ErrorCode::BUSY),
                                        |buf| {
                                            // Copy the data into the static buffer
                                            data.copy_to_slice(buf);

                                            let _ = usb.send_buffer(buf);
                                            CommandReturn::success()
                                        },
                                    )
                                })
                            })
                            .unwrap_or(CommandReturn::failure(ErrorCode::RESERVE))
                    } else {
                        CommandReturn::failure(ErrorCode::NOSUPPORT)
                    }
                })
                .unwrap_or_else(|err| err.into()),

            // Allow receive
            2 => self
                .app
                .enter(processid, |app, _| {
                    self.processid.set(processid);
                    if let Some(usb) = self.usb {
                        app.can_receive.set(true);
                        if let Some(buf) = self.recv_buffer.take() {
                            match usb.receive_buffer(buf) {
                                Ok(()) => CommandReturn::success(),
                                Err((err, buffer)) => {
                                    self.recv_buffer.replace(buffer);
                                    CommandReturn::failure(err)
                                }
                            }
                        } else {
                            CommandReturn::failure(ErrorCode::BUSY)
                        }
                    } else {
                        CommandReturn::failure(ErrorCode::NOSUPPORT)
                    }
                })
                .unwrap_or_else(|err| err.into()),

            // Cancel send
            3 => self
                .app
                .enter(processid, |_app, _| {
                    self.processid.set(processid);
                    if let Some(usb) = self.usb {
                        match usb.receive_cancel() {
                            Ok(buf) => {
                                self.recv_buffer.replace(buf);
                                CommandReturn::success()
                            }
                            Err(err) => CommandReturn::failure(err),
                        }
                    } else {
                        CommandReturn::failure(ErrorCode::NOSUPPORT)
                    }
                })
                .unwrap_or_else(|err| err.into()),

            // Cancel receive
            4 => self
                .app
                .enter(processid, |_app, _| {
                    self.processid.set(processid);
                    if let Some(usb) = self.usb {
                        match usb.receive_cancel() {
                            Ok(buf) => {
                                self.recv_buffer.replace(buf);
                                CommandReturn::success()
                            }
                            Err(err) => CommandReturn::failure(err),
                        }
                    } else {
                        CommandReturn::failure(ErrorCode::NOSUPPORT)
                    }
                })
                .unwrap_or_else(|err| err.into()),

            // Send or receive
            // This command has two parts.
            //    Part 1: Receive
            //            This will allow receives, the same as the Allow
            //            receive command above. If data is ready to receive
            //            the `packet_received()` callback will be called.
            //            When this happens the client callback will be
            //            scheduled and no send event will occur.
            //    Part 2: Send
            //            If no receive occurs we will be left in a start where
            //            future recieves will be allowed. This is the same
            //            outcome as calling the Allow receive command.
            //            As well as that we will then send the data in the
            //            send buffer.
            5 => self
                .app
                .enter(processid, |app, kernel_data| {
                    if let Some(usb) = self.usb {
                        if app.can_receive.get() {
                            // We are already receiving
                            CommandReturn::failure(ErrorCode::BUSY)
                        } else {
                            app.can_receive.set(true);
                            if let Some(buf) = self.recv_buffer.take() {
                                match usb.receive_buffer(buf) {
                                    Ok(()) => CommandReturn::success(),
                                    Err((err, buffer)) => {
                                        self.recv_buffer.replace(buffer);
                                        return CommandReturn::failure(err);
                                    }
                                }
                            } else {
                                return CommandReturn::failure(ErrorCode::BUSY);
                            };

                            if !app.can_receive.get() {
                                // The call to receive_buffer() collected a pending packet.
                                CommandReturn::failure(ErrorCode::BUSY)
                            } else {
                                kernel_data
                                    .get_readwrite_processbuffer(rw_allow::SEND)
                                    .and_then(|send| {
                                        send.enter(|data| {
                                            self.send_buffer.take().map_or(
                                                CommandReturn::failure(ErrorCode::BUSY),
                                                |buf| {
                                                    // Copy the data into the static buffer
                                                    data.copy_to_slice(buf);

                                                    let _ = usb.send_buffer(buf);
                                                    CommandReturn::success()
                                                },
                                            )
                                        })
                                    })
                                    .unwrap_or(CommandReturn::failure(ErrorCode::RESERVE))
                            }
                        }
                    } else {
                        CommandReturn::failure(ErrorCode::NOSUPPORT)
                    }
                })
                .unwrap_or_else(|err| err.into()),

            // default
            _ => CommandReturn::failure(ErrorCode::NOSUPPORT),
        }
    }

    fn allocate_grant(&self, processid: ProcessId) -> Result<(), kernel::process::Error> {
        self.app.enter(processid, |_, _| {})
    }
}