1use super::descriptors;
8use super::descriptors::Buffer64;
9use super::descriptors::DescriptorType;
10use super::descriptors::EndpointAddress;
11use super::descriptors::EndpointDescriptor;
12use super::descriptors::HIDCountryCode;
13use super::descriptors::HIDDescriptor;
14use super::descriptors::HIDSubordinateDescriptor;
15use super::descriptors::InterfaceDescriptor;
16use super::descriptors::ReportDescriptor;
17use super::descriptors::TransferDirection;
18use super::usbc_client_ctrl::ClientCtrl;
19
20use kernel::ErrorCode;
21use kernel::hil;
22use kernel::hil::usb::TransferType;
23use kernel::utilities::cells::OptionalCell;
24use kernel::utilities::cells::TakeCell;
25use tock_registers_v0::interfaces::Writeable;
26
27const ENDPOINT_NUM: usize = 1;
29
30const IN_BUFFER: usize = 0;
31
32static LANGUAGES: &[u16; 1] = &[
33 0x0409, ];
35pub const MAX_CTRL_PACKET_SIZE: u8 = 64;
37
38const N_ENDPOINTS: usize = 1;
39
40static REPORT_DESCRIPTOR: &[u8] = &[
43 0x05, 0x01, 0x09, 0x06, 0xA1, 0x01, 0x75, 0x01, 0x95, 0x08, 0x05, 0x07, 0x19, 0xE0, 0x29, 0xE7, 0x15, 0x00, 0x25, 0x01, 0x81, 0x02, 0x95, 0x01, 0x75, 0x08, 0x81, 0x03, 0x95, 0x05, 0x75, 0x01, 0x05, 0x08, 0x19, 0x01, 0x29, 0x05, 0x91, 0x02, 0x95, 0x01, 0x75, 0x03, 0x91, 0x03, 0x95, 0x06, 0x75, 0x08, 0x15, 0x00, 0x25, 0x68, 0x05, 0x07, 0x19, 0x00, 0x29, 0x68, 0x81, 0x00, 0xc0, ];
80
81static REPORT: ReportDescriptor<'static> = ReportDescriptor {
82 desc: REPORT_DESCRIPTOR,
83};
84
85static SUB_HID_DESCRIPTOR: &[HIDSubordinateDescriptor] = &[HIDSubordinateDescriptor {
86 typ: DescriptorType::Report,
87 len: REPORT_DESCRIPTOR.len() as u16,
88}];
89
90static HID_DESCRIPTOR: HIDDescriptor<'static> = HIDDescriptor {
91 hid_class: 0x0111,
92 country_code: HIDCountryCode::NotSupported,
93 sub_descriptors: SUB_HID_DESCRIPTOR,
94};
95
96pub struct KeyboardHid<'a, U: 'a> {
98 client_ctrl: ClientCtrl<'a, 'static, U>,
100
101 buffers: [Buffer64; N_ENDPOINTS],
103
104 client: OptionalCell<&'a dyn hil::usb_hid::Client<'a, [u8; 64]>>,
105
106 send_buffer: TakeCell<'static, [u8; 64]>,
108}
109
110impl<'a, U: hil::usb::UsbController<'a>> KeyboardHid<'a, U> {
111 pub fn new(
112 controller: &'a U,
113 vendor_id: u16,
114 product_id: u16,
115 strings: &'static [&'static str; 3],
116 ) -> Self {
117 let interfaces: &mut [InterfaceDescriptor] = &mut [InterfaceDescriptor {
118 interface_number: 0,
119 interface_class: 0x03, interface_subclass: 0x01, interface_protocol: 0x01, ..InterfaceDescriptor::default()
123 }];
124
125 let endpoints: &[&[EndpointDescriptor]] = &[&[EndpointDescriptor {
126 endpoint_address: EndpointAddress::new_const(
127 ENDPOINT_NUM,
128 TransferDirection::DeviceToHost,
129 ),
130 transfer_type: TransferType::Interrupt,
131 max_packet_size: 8,
132 interval: 10,
133 }]];
134
135 let (device_descriptor_buffer, other_descriptor_buffer) =
136 descriptors::create_descriptor_buffers(
137 descriptors::DeviceDescriptor {
138 vendor_id,
139 product_id,
140 manufacturer_string: 1,
141 product_string: 2,
142 serial_number_string: 3,
143 max_packet_size_ep0: MAX_CTRL_PACKET_SIZE,
144 ..descriptors::DeviceDescriptor::default()
145 },
146 descriptors::ConfigurationDescriptor {
147 attributes: descriptors::ConfigurationAttributes::new(true, true),
148 max_power: 0x32,
149 ..descriptors::ConfigurationDescriptor::default()
150 },
151 interfaces,
152 endpoints,
153 Some(&HID_DESCRIPTOR),
154 None,
155 );
156
157 KeyboardHid {
158 client_ctrl: ClientCtrl::new(
159 controller,
160 device_descriptor_buffer,
161 other_descriptor_buffer,
162 Some(&HID_DESCRIPTOR),
163 Some(&REPORT),
164 LANGUAGES,
165 strings,
166 ),
167 buffers: [Buffer64::default()],
168 client: OptionalCell::empty(),
169 send_buffer: TakeCell::empty(),
170 }
171 }
172
173 #[inline]
174 fn controller(&self) -> &'a U {
175 self.client_ctrl.controller()
176 }
177
178 pub fn set_client(&'a self, client: &'a dyn hil::usb_hid::Client<'a, [u8; 64]>) {
179 self.client.set(client);
180 }
181}
182
183impl<'a, U: hil::usb::UsbController<'a>> hil::usb_hid::UsbHid<'a, [u8; 64]> for KeyboardHid<'a, U> {
184 fn send_buffer(
185 &'a self,
186 send: &'static mut [u8; 64],
187 ) -> Result<usize, (ErrorCode, &'static mut [u8; 64])> {
188 let len = send.len();
189
190 self.send_buffer.replace(send);
191 self.controller().endpoint_resume_in(ENDPOINT_NUM);
192
193 Ok(len)
194 }
195
196 fn send_cancel(&'a self) -> Result<&'static mut [u8; 64], ErrorCode> {
197 match self.send_buffer.take() {
198 Some(buf) => Ok(buf),
199 None => Err(ErrorCode::BUSY),
200 }
201 }
202
203 fn receive_buffer(
205 &'a self,
206 _recv: &'static mut [u8; 64],
207 ) -> Result<(), (ErrorCode, &'static mut [u8; 64])> {
208 Ok(())
209 }
210
211 fn receive_cancel(&'a self) -> Result<&'static mut [u8; 64], ErrorCode> {
213 Err(ErrorCode::BUSY)
214 }
215}
216
217impl<'a, U: hil::usb::UsbController<'a>> hil::usb::Client<'a> for KeyboardHid<'a, U> {
218 fn enable(&'a self) {
219 self.client_ctrl.enable();
221
222 self.controller()
224 .endpoint_set_in_buffer(ENDPOINT_NUM, &self.buffers[IN_BUFFER].buf);
225 self.controller()
226 .endpoint_in_out_enable(TransferType::Interrupt, ENDPOINT_NUM);
227 }
228
229 fn attach(&'a self) {
230 self.client_ctrl.attach();
231 }
232
233 fn bus_reset(&'a self) {}
234
235 fn ctrl_setup(&'a self, endpoint: usize) -> hil::usb::CtrlSetupResult {
237 self.client_ctrl.ctrl_setup(endpoint)
238 }
239
240 fn ctrl_in(&'a self, endpoint: usize) -> hil::usb::CtrlInResult {
242 self.client_ctrl.ctrl_in(endpoint)
243 }
244
245 fn ctrl_out(&'a self, _endpoint: usize, _packet_bytes: u32) -> hil::usb::CtrlOutResult {
247 hil::usb::CtrlOutResult::Ok
249 }
250
251 fn ctrl_status(&'a self, endpoint: usize) {
252 self.client_ctrl.ctrl_status(endpoint)
253 }
254
255 fn ctrl_status_complete(&'a self, endpoint: usize) {
257 self.client_ctrl.ctrl_status_complete(endpoint)
258 }
259
260 fn packet_in(&'a self, transfer_type: TransferType, _endpoint: usize) -> hil::usb::InResult {
269 match transfer_type {
270 TransferType::Interrupt => {
271 self.send_buffer
272 .take()
273 .map_or(hil::usb::InResult::Delay, |buf| {
274 let packet = &self.buffers[IN_BUFFER].buf;
277
278 for i in 0..8 {
282 packet[i].set(buf[i]);
283 }
284
285 self.send_buffer.replace(buf);
288
289 hil::usb::InResult::Packet(8)
291 })
292 }
293 TransferType::Bulk | TransferType::Control | TransferType::Isochronous => {
294 hil::usb::InResult::Error
295 }
296 }
297 }
298
299 fn packet_out(
303 &'a self,
304 transfer_type: TransferType,
305 _endpoint: usize,
306 _packet_bytes: u32,
307 ) -> hil::usb::OutResult {
308 match transfer_type {
309 TransferType::Interrupt => hil::usb::OutResult::Ok,
310
311 TransferType::Bulk | TransferType::Control | TransferType::Isochronous => {
312 hil::usb::OutResult::Error
313 }
314 }
315 }
316
317 fn packet_transmitted(&'a self, endpoint: usize) {
318 self.send_buffer.take().map(|buf| {
319 self.client.map(move |client| {
320 client.packet_transmitted(Ok(()), buf, endpoint);
321 });
322 });
323 }
324}