1use core::cell::Cell;
10use core::cmp::min;
11use core::fmt;
12
13use kernel::hil::usb::TransferType;
14use tock_registers_v0::interfaces::Readable;
15use tock_registers_v0::registers::InMemoryRegister;
16
17#[repr(align(4))]
20pub struct Buffer8 {
21 pub buf: [InMemoryRegister<u8>; 8],
22}
23
24impl Default for Buffer8 {
25 fn default() -> Self {
26 Self {
27 buf: [const { InMemoryRegister::new(0) }; 8],
28 }
29 }
30}
31
32#[repr(align(4))]
33pub struct Buffer64 {
34 pub buf: [InMemoryRegister<u8>; 64],
35}
36
37impl Default for Buffer64 {
38 fn default() -> Self {
39 Self {
40 buf: [const { InMemoryRegister::new(0) }; 64],
41 }
42 }
43}
44
45#[derive(Debug, Copy, Clone)]
47pub struct SetupData {
48 pub request_type: DeviceRequestType,
49 pub request_code: u8,
50 pub value: u16,
51 pub index: u16,
52 pub length: u16,
53}
54
55impl SetupData {
56 pub fn get(p: &[InMemoryRegister<u8>]) -> Option<Self> {
58 if p.len() < 8 {
59 return None;
60 }
61 Some(SetupData {
62 request_type: DeviceRequestType(p[0].get()),
63 request_code: p[1].get(),
64 value: get_u16(p[2].get(), p[3].get()),
65 index: get_u16(p[4].get(), p[5].get()),
66 length: get_u16(p[6].get(), p[7].get()),
67 })
68 }
69
70 pub fn get_standard_request(&self) -> Option<StandardRequest> {
72 match self.request_type.request_type() {
73 RequestType::Standard => match self.request_code {
74 0 => Some(StandardRequest::GetStatus {
75 recipient_index: self.index,
76 }),
77 1 => Some(StandardRequest::ClearFeature {
78 feature: FeatureSelector::get(self.value),
79 recipient_index: self.index,
80 }),
81 3 => Some(StandardRequest::SetFeature {
82 feature: FeatureSelector::get(self.value),
83 test_mode: (self.index >> 8) as u8,
84 recipient_index: self.index & 0xff,
85 }),
86 5 => Some(StandardRequest::SetAddress {
87 device_address: self.value,
88 }),
89 6 => get_descriptor_type((self.value >> 8) as u8).map(|dt| {
90 StandardRequest::GetDescriptor {
91 descriptor_type: dt,
92 descriptor_index: (self.value & 0xff) as u8,
93 lang_id: self.index,
94 requested_length: self.length,
95 }
96 }),
97 7 => get_set_descriptor_type((self.value >> 8) as u8).map(|dt| {
98 StandardRequest::SetDescriptor {
99 descriptor_type: dt,
100 descriptor_index: (self.value & 0xff) as u8,
101 lang_id: self.index,
102 descriptor_length: self.length,
103 }
104 }),
105 8 => Some(StandardRequest::GetConfiguration),
106 9 => Some(StandardRequest::SetConfiguration {
107 configuration_value: (self.value & 0xff) as u8,
108 }),
109 10 => Some(StandardRequest::GetInterface {
110 interface: self.index,
111 }),
112 11 => Some(StandardRequest::SetInterface),
113 12 => Some(StandardRequest::SynchFrame),
114 _ => None,
115 },
116 _ => None,
117 }
118 }
119}
120
121#[derive(Debug)]
122pub enum StandardRequest {
123 GetStatus {
124 recipient_index: u16,
125 },
126 ClearFeature {
127 feature: FeatureSelector,
128 recipient_index: u16,
129 },
130 SetFeature {
131 feature: FeatureSelector,
132 test_mode: u8,
133 recipient_index: u16,
134 },
135 SetAddress {
136 device_address: u16,
137 },
138 GetDescriptor {
139 descriptor_type: DescriptorType,
140 descriptor_index: u8,
141 lang_id: u16,
142 requested_length: u16,
143 },
144 SetDescriptor {
145 descriptor_type: DescriptorType,
146 descriptor_index: u8,
147 lang_id: u16,
148 descriptor_length: u16,
149 },
150 GetConfiguration,
151 SetConfiguration {
152 configuration_value: u8,
153 },
154 GetInterface {
155 interface: u16,
156 },
157 SetInterface,
158 SynchFrame,
159}
160
161#[derive(Copy, Clone, Debug)]
162pub enum DescriptorType {
163 Device = 1,
164 Configuration,
165 String,
166 Interface,
167 Endpoint,
168 DeviceQualifier,
169 OtherSpeedConfiguration,
170 InterfacePower,
171 HID = 0x21,
172 Report = 0x22,
173 CdcInterface = 0x24,
174}
175
176fn get_descriptor_type(byte: u8) -> Option<DescriptorType> {
177 match byte {
178 1 => Some(DescriptorType::Device),
179 2 => Some(DescriptorType::Configuration),
180 3 => Some(DescriptorType::String),
181 4 => Some(DescriptorType::Interface),
182 5 => Some(DescriptorType::Endpoint),
183 6 => Some(DescriptorType::DeviceQualifier),
184 7 => Some(DescriptorType::OtherSpeedConfiguration),
185 8 => Some(DescriptorType::InterfacePower),
186 0x21 => Some(DescriptorType::HID),
187 0x22 => Some(DescriptorType::Report),
188 0x24 => Some(DescriptorType::CdcInterface),
189 _ => None,
190 }
191}
192
193fn get_set_descriptor_type(byte: u8) -> Option<DescriptorType> {
195 match get_descriptor_type(byte) {
196 dt @ Some(DescriptorType::Device) => dt,
197 dt @ Some(DescriptorType::Configuration) => dt,
198 dt @ Some(DescriptorType::String) => dt,
199 _ => None,
200 }
201}
202
203#[derive(Copy, Clone)]
204pub struct DeviceRequestType(u8);
205
206impl DeviceRequestType {
207 pub fn transfer_direction(self) -> TransferDirection {
208 match self.0 & (1 << 7) {
209 0 => TransferDirection::HostToDevice,
210 _ => TransferDirection::DeviceToHost,
211 }
212 }
213
214 pub fn request_type(self) -> RequestType {
215 match (self.0 & (0b11 << 5)) >> 5 {
216 0 => RequestType::Standard,
217 1 => RequestType::Class,
218 2 => RequestType::Vendor,
219 _ => RequestType::Reserved,
220 }
221 }
222
223 pub fn recipient(self) -> Recipient {
224 match self.0 & 0b11111 {
225 0 => Recipient::Device,
226 1 => Recipient::Interface,
227 2 => Recipient::Endpoint,
228 3 => Recipient::Other,
229 _ => Recipient::Reserved,
230 }
231 }
232}
233
234impl fmt::Debug for DeviceRequestType {
235 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
236 write!(
237 f,
238 "{{{:?}, {:?}, {:?}}}",
239 self.transfer_direction(),
240 self.request_type(),
241 self.recipient()
242 )
243 }
244}
245
246#[derive(Debug)]
247pub enum TransferDirection {
248 HostToDevice = 0,
249 DeviceToHost = 1,
250}
251
252#[derive(Debug)]
253pub enum RequestType {
254 Standard,
255 Class,
256 Vendor,
257 Reserved,
258}
259
260#[derive(Debug)]
261pub enum Recipient {
262 Device,
263 Interface,
264 Endpoint,
265 Other,
266 Reserved,
267}
268
269#[derive(Debug)]
270pub enum FeatureSelector {
271 DeviceRemoteWakeup,
272 EndpointHalt,
273 TestMode,
274 Unknown,
275}
276
277impl FeatureSelector {
278 fn get(value: u16) -> Self {
279 match value {
280 1 => FeatureSelector::DeviceRemoteWakeup,
281 0 => FeatureSelector::EndpointHalt,
282 2 => FeatureSelector::TestMode,
283 _ => FeatureSelector::Unknown,
284 }
285 }
286}
287
288pub trait Descriptor {
289 fn size(&self) -> usize;
291
292 fn write_to(&self, buf: &[Cell<u8>]) -> usize {
294 if self.size() > buf.len() {
295 0
296 } else {
297 self.write_to_unchecked(buf)
298 }
299 }
300
301 fn write_to_unchecked(&self, buf: &[Cell<u8>]) -> usize;
305}
306
307pub struct DeviceDescriptor {
308 pub usb_release: u16,
310
311 pub class: u8,
315
316 pub subclass: u8,
318
319 pub protocol: u8,
321
322 pub max_packet_size_ep0: u8,
324
325 pub vendor_id: u16,
327
328 pub product_id: u16,
330
331 pub device_release: u16,
333
334 pub manufacturer_string: u8,
336
337 pub product_string: u8,
339
340 pub serial_number_string: u8,
342
343 pub num_configurations: u8,
345}
346
347impl Default for DeviceDescriptor {
348 fn default() -> Self {
349 DeviceDescriptor {
350 usb_release: 0x0200,
351 class: 0,
352 subclass: 0,
353 protocol: 0,
354 max_packet_size_ep0: 8,
355 vendor_id: 0x6667,
356 product_id: 0xabcd,
357 device_release: 0x0001,
358 manufacturer_string: 0,
359 product_string: 0,
360 serial_number_string: 0,
361 num_configurations: 1,
362 }
363 }
364}
365
366impl Descriptor for DeviceDescriptor {
367 fn size(&self) -> usize {
368 18
369 }
370
371 fn write_to_unchecked(&self, buf: &[Cell<u8>]) -> usize {
372 buf[0].set(18); buf[1].set(DescriptorType::Device as u8);
374 put_u16(&buf[2..4], self.usb_release);
375 buf[4].set(self.class);
376 buf[5].set(self.subclass);
377 buf[6].set(self.protocol);
378 buf[7].set(self.max_packet_size_ep0);
379 put_u16(&buf[8..10], self.vendor_id);
380 put_u16(&buf[10..12], self.product_id);
381 put_u16(&buf[12..14], self.device_release);
382 buf[14].set(self.manufacturer_string);
383 buf[15].set(self.product_string);
384 buf[16].set(self.serial_number_string);
385 buf[17].set(self.num_configurations);
386 18
387 }
388}
389
390pub struct DeviceBuffer {
394 pub buf: [Cell<u8>; 19],
395 pub len: usize,
396}
397
398impl DeviceBuffer {
399 pub fn write_to(&self, buf: &[Cell<u8>]) -> usize {
400 for i in 0..self.len {
401 buf[i].set(self.buf[i].get());
402 }
403 self.len
404 }
405}
406
407pub struct DescriptorBuffer {
410 pub buf: [Cell<u8>; 128],
411 pub len: usize,
412}
413
414impl DescriptorBuffer {
415 pub fn write_to(&self, buf: &[Cell<u8>]) -> usize {
416 for i in 0..self.len {
417 buf[i].set(self.buf[i].get());
418 }
419 self.len
420 }
421}
422
423pub fn create_descriptor_buffers(
433 device_descriptor: DeviceDescriptor,
434 mut configuration_descriptor: ConfigurationDescriptor,
435 interface_descriptor: &mut [InterfaceDescriptor],
436 endpoint_descriptors: &[&[EndpointDescriptor]],
437 hid_descriptor: Option<&HIDDescriptor>,
438 cdc_descriptor: Option<&[CdcInterfaceDescriptor]>,
439) -> (DeviceBuffer, DescriptorBuffer) {
440 let mut dev_buf = DeviceBuffer {
443 buf: [
444 Cell::default(),
445 Cell::default(),
446 Cell::default(),
447 Cell::default(),
448 Cell::default(),
449 Cell::default(),
450 Cell::default(),
451 Cell::default(),
452 Cell::default(),
453 Cell::default(),
454 Cell::default(),
455 Cell::default(),
456 Cell::default(),
457 Cell::default(),
458 Cell::default(),
459 Cell::default(),
460 Cell::default(),
461 Cell::default(),
462 Cell::default(),
463 ],
464 len: 0,
465 };
466 dev_buf.len = device_descriptor.write_to(&dev_buf.buf);
467
468 let mut other_buf = DescriptorBuffer {
473 #[rustfmt::skip]
474 buf: [
475 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
476 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
477 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
478 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
479 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
480 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
481 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
482 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
483 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
484 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
485 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
486 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
487 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
488 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
489 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
490 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
491 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
492 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
493 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
494 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
495 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
496 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
497 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
498 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
499 Cell::default(), Cell::default(), Cell::default(), Cell::default(), Cell::default(),
500 Cell::default(), Cell::default(), Cell::default(),
501 ],
502 len: 0,
503 };
504
505 configuration_descriptor.num_interfaces = interface_descriptor.len() as u8;
511
512 configuration_descriptor.related_descriptor_length =
516 interface_descriptor.iter().map(|d| d.size()).sum::<usize>()
517 + endpoint_descriptors
518 .iter()
519 .map(|descs| descs.iter().map(|d| d.size()).sum::<usize>())
520 .sum::<usize>()
521 + hid_descriptor.map_or(0, |d| d.size())
522 + cdc_descriptor.map_or(0, |ds| ds.iter().map(|d| d.size()).sum::<usize>());
523
524 for (i, d) in interface_descriptor.iter_mut().enumerate() {
526 d.num_endpoints = endpoint_descriptors[i].len() as u8;
527 }
528
529 let mut len = 0;
531 len += configuration_descriptor.write_to(&other_buf.buf[len..]);
532
533 for (i, d) in interface_descriptor.iter().enumerate() {
535 len += d.write_to(&other_buf.buf[len..]);
537
538 if i == 0 {
541 if let Some(dh) = hid_descriptor {
543 len += dh.write_to(&other_buf.buf[len..]);
544 }
545 }
546
547 if i == 0 {
550 if let Some(dcdc) = cdc_descriptor {
552 for dcs in dcdc {
553 len += dcs.write_to(&other_buf.buf[len..]);
554 }
555 }
556 }
557
558 for de in endpoint_descriptors[i] {
560 len += de.write_to(&other_buf.buf[len..]);
561 }
562 }
563 other_buf.len = min(len, other_buf.buf.len());
564
565 (dev_buf, other_buf)
567}
568
569pub struct ConfigurationDescriptor {
570 pub num_interfaces: u8,
571 pub configuration_value: u8,
572 pub string_index: u8,
573 pub attributes: ConfigurationAttributes,
574 pub max_power: u8, pub related_descriptor_length: usize,
576}
577
578impl Default for ConfigurationDescriptor {
579 fn default() -> Self {
580 ConfigurationDescriptor {
581 num_interfaces: 1,
582 configuration_value: 1,
583 string_index: 0,
584 attributes: ConfigurationAttributes::new(true, false),
585 max_power: 0, related_descriptor_length: 0,
587 }
588 }
589}
590
591impl Descriptor for ConfigurationDescriptor {
592 fn size(&self) -> usize {
593 9
594 }
595
596 fn write_to_unchecked(&self, buf: &[Cell<u8>]) -> usize {
597 buf[0].set(9); buf[1].set(DescriptorType::Configuration as u8);
599 put_u16(&buf[2..4], (9 + self.related_descriptor_length) as u16);
600 buf[4].set(self.num_interfaces);
601 buf[5].set(self.configuration_value);
602 buf[6].set(self.string_index);
603 buf[7].set(From::from(self.attributes));
604 buf[8].set(self.max_power);
605 9
606 }
607}
608
609#[derive(Copy, Clone)]
610pub struct ConfigurationAttributes(u8);
611
612impl ConfigurationAttributes {
613 pub fn new(is_self_powered: bool, supports_remote_wakeup: bool) -> Self {
614 ConfigurationAttributes(
615 (1 << 7)
616 | if is_self_powered { 1 << 6 } else { 0 }
617 | if supports_remote_wakeup { 1 << 5 } else { 0 },
618 )
619 }
620}
621
622impl From<ConfigurationAttributes> for u8 {
623 fn from(ca: ConfigurationAttributes) -> u8 {
624 ca.0
625 }
626}
627
628pub struct InterfaceDescriptor {
629 pub interface_number: u8,
630 pub alternate_setting: u8,
631 pub num_endpoints: u8,
632 pub interface_class: u8,
633 pub interface_subclass: u8,
634 pub interface_protocol: u8,
635 pub string_index: u8,
636}
637
638impl Default for InterfaceDescriptor {
639 fn default() -> Self {
640 InterfaceDescriptor {
641 interface_number: 0,
642 alternate_setting: 0,
643 num_endpoints: 0, interface_class: 0xff, interface_subclass: 0xab,
646 interface_protocol: 0,
647 string_index: 0,
648 }
649 }
650}
651
652impl Descriptor for InterfaceDescriptor {
653 fn size(&self) -> usize {
654 9
655 }
656
657 fn write_to_unchecked(&self, buf: &[Cell<u8>]) -> usize {
658 buf[0].set(9); buf[1].set(DescriptorType::Interface as u8);
660 buf[2].set(self.interface_number);
661 buf[3].set(self.alternate_setting);
662 buf[4].set(self.num_endpoints);
663 buf[5].set(self.interface_class);
664 buf[6].set(self.interface_subclass);
665 buf[7].set(self.interface_protocol);
666 buf[8].set(self.string_index);
667 9
668 }
669}
670
671pub struct EndpointAddress(u8);
672
673impl EndpointAddress {
674 pub fn new(endpoint: usize, direction: TransferDirection) -> Self {
675 EndpointAddress(
676 endpoint as u8 & 0xf
677 | match direction {
678 TransferDirection::HostToDevice => 0,
679 TransferDirection::DeviceToHost => 1,
680 } << 7,
681 )
682 }
683
684 pub const fn new_const(endpoint: usize, direction: TransferDirection) -> Self {
688 EndpointAddress(endpoint as u8 & 0xf | (direction as u8) << 7)
689 }
690}
691
692pub struct EndpointDescriptor {
693 pub endpoint_address: EndpointAddress,
694 pub transfer_type: TransferType,
695 pub max_packet_size: u16,
696 pub interval: u8,
698}
699
700impl Descriptor for EndpointDescriptor {
701 fn size(&self) -> usize {
702 7
703 }
704
705 fn write_to_unchecked(&self, buf: &[Cell<u8>]) -> usize {
706 let len = self.size();
707 buf[0].set(len as u8);
708 buf[1].set(DescriptorType::Endpoint as u8);
709 buf[2].set(self.endpoint_address.0);
710 buf[3].set(self.transfer_type as u8);
713 put_u16(&buf[4..6], self.max_packet_size & 0x7ff_u16);
714 buf[6].set(self.interval);
715 len
716 }
717}
718
719#[derive(Copy, Clone)]
720pub enum HIDCountryCode {
721 NotSupported = 0,
722 Arabic,
723 Belgian,
724 CanadianBilingual,
725 CanadianFrench,
726 CzechRepublic,
727 Danish,
728 Finnish,
729 French,
730 German,
731 Greek,
732 Hebrew,
733 Hungary,
734 InternationalISO,
735 Italian,
736 JapanKatakana,
737 Korean,
738 LatinAmerican,
739 NetherlandsDutch,
740 Norwegian,
741 PersianFarsi,
742 Poland,
743 Portuguese,
744 Russia,
745 Slovakia,
746 Spanish,
747 Swedish,
748 SwissFrench,
749 SwissGerman,
750 Switzerland,
751 Taiwan,
752 TurkishQ,
753 UK,
754 US,
755 Yugoslavia,
756 TurkishF,
757}
758
759pub struct HIDDescriptor<'a> {
760 pub hid_class: u16,
761 pub country_code: HIDCountryCode,
762 pub sub_descriptors: &'a [HIDSubordinateDescriptor],
763}
764
765pub struct HIDSubordinateDescriptor {
766 pub typ: DescriptorType,
767 pub len: u16,
768}
769
770impl Descriptor for HIDDescriptor<'_> {
771 fn size(&self) -> usize {
772 6 + (3 * self.sub_descriptors.len())
773 }
774
775 fn write_to_unchecked(&self, buf: &[Cell<u8>]) -> usize {
776 let len = self.size();
777 buf[0].set(len as u8);
778 buf[1].set(DescriptorType::HID as u8);
779 put_u16(&buf[2..4], self.hid_class);
780 buf[4].set(self.country_code as u8);
781 buf[5].set(self.sub_descriptors.len() as u8);
782 for (i, desc) in self.sub_descriptors.iter().enumerate() {
783 buf[6 + 3 * i].set(desc.typ as u8);
784 put_u16(&buf[7 + (3 * i)..9 + (3 * i)], desc.len);
785 }
786 len
787 }
788}
789
790pub struct ReportDescriptor<'a> {
791 pub desc: &'a [u8],
792}
793
794impl Descriptor for ReportDescriptor<'_> {
795 fn size(&self) -> usize {
796 self.desc.len()
797 }
798
799 fn write_to_unchecked(&self, buf: &[Cell<u8>]) -> usize {
800 for (i, x) in self.desc.iter().enumerate() {
801 buf[i].set(*x);
802 }
803 self.size()
804 }
805}
806
807#[derive(Copy, Clone)]
812pub enum CdcInterfaceDescriptorSubType {
813 Header = 0x00,
814 CallManagement = 0x01,
815 AbstractControlManagement = 0x02,
816 DirectLineManagement = 0x03,
817 TelephoneRinger = 0x04,
818 TelephoneCallLineStateReportingCapbailities = 0x05,
819 Union = 0x06,
820 CountrySelection = 0x07,
821 TelephoneOperationalModes = 0x08,
822 UsbTerminal = 0x09,
823 NetworkChannelTerminal = 0x0a,
824 ProtocolUnit = 0x0b,
825 ExtensionUnity = 0x0c,
826 MultiChannelManagement = 0x0d,
827 CapiControlManagement = 0x0e,
828 EthernetNetworking = 0x0f,
829 AtmNetworking = 0x10,
830}
831
832pub struct CdcInterfaceDescriptor {
833 pub subtype: CdcInterfaceDescriptorSubType,
834 pub field1: u8,
835 pub field2: u8,
836}
837
838impl Descriptor for CdcInterfaceDescriptor {
839 fn size(&self) -> usize {
840 3 + match self.subtype {
841 CdcInterfaceDescriptorSubType::Header => 2,
842 CdcInterfaceDescriptorSubType::CallManagement => 2,
843 CdcInterfaceDescriptorSubType::AbstractControlManagement => 1,
844 CdcInterfaceDescriptorSubType::DirectLineManagement => 1,
845 CdcInterfaceDescriptorSubType::TelephoneRinger => 2,
846 CdcInterfaceDescriptorSubType::TelephoneCallLineStateReportingCapbailities => 4,
847 CdcInterfaceDescriptorSubType::Union => 2,
848 CdcInterfaceDescriptorSubType::CountrySelection => 2,
849 CdcInterfaceDescriptorSubType::TelephoneOperationalModes => 1,
850 CdcInterfaceDescriptorSubType::UsbTerminal => 1,
851 CdcInterfaceDescriptorSubType::NetworkChannelTerminal => 1,
852 CdcInterfaceDescriptorSubType::ProtocolUnit => 1,
853 CdcInterfaceDescriptorSubType::ExtensionUnity => 1,
854 CdcInterfaceDescriptorSubType::MultiChannelManagement => 1,
855 CdcInterfaceDescriptorSubType::CapiControlManagement => 1,
856 CdcInterfaceDescriptorSubType::EthernetNetworking => 1,
857 CdcInterfaceDescriptorSubType::AtmNetworking => 1,
858 }
859 }
860
861 fn write_to_unchecked(&self, buf: &[Cell<u8>]) -> usize {
862 let len = self.size();
863 buf[0].set(len as u8);
864 buf[1].set(DescriptorType::CdcInterface as u8);
865 buf[2].set(self.subtype as u8);
866 if len >= 4 {
867 buf[3].set(self.field1);
868 }
869 if len >= 5 {
870 buf[4].set(self.field2);
871 }
872 len
873 }
874}
875
876#[derive(Debug, Copy, Clone)]
878pub struct CdcAcmSetLineCodingData {
879 pub baud_rate: u32,
880 pub stop_bits: u8,
881 pub parity: u8,
882 pub data_bits: u8,
883}
884
885impl CdcAcmSetLineCodingData {
886 pub fn get(p: &[InMemoryRegister<u8>]) -> Option<Self> {
889 if p.len() < 7 {
890 return None;
891 }
892 Some(CdcAcmSetLineCodingData {
893 baud_rate: get_u32(p[0].get(), p[1].get(), p[2].get(), p[3].get()),
894 stop_bits: p[4].get(),
895 parity: p[5].get(),
896 data_bits: p[6].get(),
897 })
898 }
899}
900
901impl Descriptor for CdcAcmSetLineCodingData {
902 fn size(&self) -> usize {
903 7
904 }
905
906 fn write_to_unchecked(&self, buf: &[Cell<u8>]) -> usize {
907 let len = self.size();
908 buf[0].set(self.baud_rate as u8);
909 buf[1].set((self.baud_rate >> 8) as u8);
910 buf[2].set((self.baud_rate >> 16) as u8);
911 buf[3].set((self.baud_rate >> 24) as u8);
912 buf[4].set(self.stop_bits);
913 buf[5].set(self.parity);
914 buf[6].set(self.data_bits);
915 len
916 }
917}
918
919impl Default for CdcAcmSetLineCodingData {
920 fn default() -> Self {
921 CdcAcmSetLineCodingData {
923 baud_rate: 115200,
924 stop_bits: 0,
925 parity: 0,
926 data_bits: 8,
927 }
928 }
929}
930
931pub struct LanguagesDescriptor<'a> {
932 pub langs: &'a [u16],
933}
934
935impl Descriptor for LanguagesDescriptor<'_> {
936 fn size(&self) -> usize {
937 2 + (2 * self.langs.len())
938 }
939
940 fn write_to_unchecked(&self, buf: &[Cell<u8>]) -> usize {
941 let len = self.size();
942 buf[0].set(len as u8);
943 buf[1].set(DescriptorType::String as u8);
944 for (i, lang) in self.langs.iter().enumerate() {
945 put_u16(&buf[2 + (2 * i)..4 + (2 * i)], *lang);
946 }
947 len
948 }
949}
950
951pub struct StringDescriptor<'a> {
952 pub string: &'a str,
953}
954
955impl Descriptor for StringDescriptor<'_> {
956 fn size(&self) -> usize {
957 let mut len = 2;
958 for ch in self.string.chars() {
959 len += 2 * ch.len_utf16();
960 }
961 len
962 }
963
964 fn write_to_unchecked(&self, buf: &[Cell<u8>]) -> usize {
966 buf[1].set(DescriptorType::String as u8);
967 let mut i = 2;
968 for ch in self.string.chars() {
969 let mut chbuf = [0; 2];
970 for w in ch.encode_utf16(&mut chbuf) {
971 put_u16(&buf[i..i + 2], *w);
972 i += 2;
973 }
974 }
975 buf[0].set(i as u8);
976 i
977 }
978}
979
980fn get_u16(b0: u8, b1: u8) -> u16 {
982 (b0 as u16) | ((b1 as u16) << 8)
983}
984
985fn get_u32(b0: u8, b1: u8, b2: u8, b3: u8) -> u32 {
987 (b0 as u32) | ((b1 as u32) << 8) | ((b2 as u32) << 16) | ((b3 as u32) << 24)
988}
989
990fn put_u16(buf: &[Cell<u8>], n: u16) {
992 buf[0].set((n & 0xff) as u8);
993 buf[1].set((n >> 8) as u8);
994}