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
use core::cell::Cell;
use kernel::{AppId, Callback, Driver, Grant, ReturnCode};
use kernel::hil;
pub const DRIVER_NUM: usize = 0x20005;
#[derive(Default)]
pub struct App {
callback: Option<Callback>,
awaiting: Option<Request>,
}
pub struct UsbSyscallDriver<'a, C: hil::usb::Client + 'a> {
usbc_client: &'a C,
apps: Grant<App>,
serving_app: Cell<Option<AppId>>,
}
impl<'a, C> UsbSyscallDriver<'a, C>
where
C: hil::usb::Client,
{
pub fn new(usbc_client: &'a C, apps: Grant<App>) -> Self {
UsbSyscallDriver {
usbc_client: usbc_client,
apps: apps,
serving_app: Cell::new(None),
}
}
fn serve_waiting_apps(&self) {
if self.serving_app.get().is_some() {
return;
}
let mut found = false;
for app in self.apps.iter() {
app.enter(|app, _| {
if let Some(request) = app.awaiting {
found = true;
match request {
Request::EnableAndAttach => {
self.usbc_client.enable();
self.usbc_client.attach();
if let Some(mut callback) = app.callback {
callback.schedule(From::from(ReturnCode::SUCCESS), 0, 0);
}
app.awaiting = None;
}
}
}
});
if found {
break;
}
}
if !found {
}
}
}
#[derive(Copy, Clone)]
enum Request {
EnableAndAttach,
}
impl<'a, C> Driver for UsbSyscallDriver<'a, C>
where
C: hil::usb::Client,
{
fn subscribe(&self, subscribe_num: usize, callback: Callback) -> ReturnCode {
match subscribe_num {
0 => self.apps
.enter(callback.app_id(), |app, _| {
app.callback = Some(callback);
ReturnCode::SUCCESS
})
.unwrap_or_else(|err| err.into()),
_ => ReturnCode::ENOSUPPORT,
}
}
fn command(&self, command_num: usize, _arg: usize, _: usize, appid: AppId) -> ReturnCode {
match command_num {
0 => ReturnCode::SUCCESS,
1 => {
let result = self.apps
.enter(appid, |app, _| {
if app.awaiting.is_some() {
ReturnCode::EBUSY
} else {
if app.callback.is_some() {
app.awaiting = Some(Request::EnableAndAttach);
ReturnCode::SUCCESS
} else {
ReturnCode::EINVAL
}
}
})
.unwrap_or_else(|err| err.into());
if result == ReturnCode::SUCCESS {
self.serve_waiting_apps();
}
result
}
_ => ReturnCode::ENOSUPPORT,
}
}
}