|
|
@@ -25,11 +25,26 @@ const AUDIO_STREAMING_FORMAT_TYPE: u8 = 0x02;
|
|
25
|
25
|
|
|
26
|
26
|
const AUDIO_ENDPOINT_GENERAL: u8 = 0x01;
|
|
27
|
27
|
|
|
|
28
|
+const SET_CUR: u8 = 0x01;
|
|
|
29
|
+const GET_CUR: u8 = 0x81;
|
|
|
30
|
+/*const SET_MIN: u8 = 0x02;*/
|
|
|
31
|
+const GET_MIN: u8 = 0x82;
|
|
|
32
|
+/*const SET_MAX: u8 = 0x03;*/
|
|
|
33
|
+const GET_MAX: u8 = 0x83;
|
|
|
34
|
+/*const SET_RES: u8 = 0x04;*/
|
|
|
35
|
+const GET_RES: u8 = 0x84;
|
|
|
36
|
+/*const SET_MEM: u8 = 0x05;
|
|
|
37
|
+const GET_MEM: u8 = 0x85;
|
|
|
38
|
+const GET_STAT: u8 = 0xFF;*/
|
|
|
39
|
+
|
|
|
40
|
+const MUTE_CONTROL: u8 = 0x01;
|
|
|
41
|
+const VOLUME_CONTROL: u8 = 0x02;
|
|
|
42
|
+
|
|
28
|
43
|
// TODO: Update bDelay.
|
|
29
|
44
|
|
|
30
|
45
|
pub struct UsbAudioClass<'a, B: UsbBus, OUTBUF: AsRef<[u32]>> {
|
|
31
|
46
|
audio_control: InterfaceNumber,
|
|
32
|
|
- audio_streaming_inactive: InterfaceNumber,
|
|
|
47
|
+ //audio_streaming_inactive: InterfaceNumber,
|
|
33
|
48
|
audio_streaming: InterfaceNumber,
|
|
34
|
49
|
audio_out: EndpointOut<'a, B>,
|
|
35
|
50
|
//audio_out_sync: EndpointIn<'a, B>,
|
|
|
@@ -37,13 +52,16 @@ pub struct UsbAudioClass<'a, B: UsbBus, OUTBUF: AsRef<[u32]>> {
|
|
37
|
52
|
samples_per_frame: [u8; 3],
|
|
38
|
53
|
|
|
39
|
54
|
audio_out_buf: Option<OUTBUF>,
|
|
|
55
|
+
|
|
|
56
|
+ volume: [u8; 2],
|
|
|
57
|
+ mute: u8,
|
|
40
|
58
|
}
|
|
41
|
59
|
|
|
42
|
60
|
impl<'a, B: UsbBus, OUTBUF: AsRef<[u32]>> UsbAudioClass<'a, B, OUTBUF> {
|
|
43
|
61
|
pub fn new(bus_alloc: &'a UsbBusAllocator<B>) -> Self {
|
|
44
|
62
|
UsbAudioClass {
|
|
45
|
63
|
audio_control: bus_alloc.interface(),
|
|
46
|
|
- audio_streaming_inactive: bus_alloc.interface(),
|
|
|
64
|
+ //audio_streaming_inactive: bus_alloc.interface(),
|
|
47
|
65
|
audio_streaming: bus_alloc.interface(),
|
|
48
|
66
|
// 48kHz * 2 * 16bit = 192B per packet. We allocate a bit more in case the device clock
|
|
49
|
67
|
// is faster than the host clock.
|
|
|
@@ -73,6 +91,9 @@ impl<'a, B: UsbBus, OUTBUF: AsRef<[u32]>> UsbAudioClass<'a, B, OUTBUF> {
|
|
73
|
91
|
),*/
|
|
74
|
92
|
samples_per_frame: [48 >> 2, 48 << 6, 0], // 48 kHz
|
|
75
|
93
|
audio_out_buf: None,
|
|
|
94
|
+
|
|
|
95
|
+ volume: [1, 0],
|
|
|
96
|
+ mute: 0,
|
|
76
|
97
|
}
|
|
77
|
98
|
}
|
|
78
|
99
|
|
|
|
@@ -159,7 +180,7 @@ impl<B: UsbBus, OUTBUF: AsRef<[u32]>> UsbClass<B> for UsbAudioClass<'_, B, OUTBU
|
|
159
|
180
|
|
|
160
|
181
|
// Audio streaming interface (zero-bandwidth).
|
|
161
|
182
|
writer.interface(
|
|
162
|
|
- self.audio_streaming_inactive,
|
|
|
183
|
+ self.audio_streaming,
|
|
163
|
184
|
DEVICE_CLASS_AUDIO,
|
|
164
|
185
|
AUDIO_SUBCLASS_STREAMING,
|
|
165
|
186
|
AUDIO_PROTOCOL_NONE,
|
|
|
@@ -195,6 +216,9 @@ impl<B: UsbBus, OUTBUF: AsRef<[u32]>> UsbClass<B> for UsbAudioClass<'_, B, OUTBU
|
|
195
|
216
|
0x02, // bSubframeSize
|
|
196
|
217
|
0x10, // bBitResolution
|
|
197
|
218
|
0x01, // bSamFreqType
|
|
|
219
|
+ 0x80, // tSamFreq[1]
|
|
|
220
|
+ 0xbb, //
|
|
|
221
|
+ 0x00, //
|
|
198
|
222
|
],
|
|
199
|
223
|
)?;
|
|
200
|
224
|
|
|
|
@@ -243,15 +267,64 @@ impl<B: UsbBus, OUTBUF: AsRef<[u32]>> UsbClass<B> for UsbAudioClass<'_, B, OUTBU
|
|
243
|
267
|
fn control_out(&mut self, xfer: ControlOut<B>) {
|
|
244
|
268
|
let req = xfer.request();
|
|
245
|
269
|
|
|
246
|
|
- if !(req.request_type == control::RequestType::Class
|
|
|
270
|
+ // TODO: Alternate interfaces?
|
|
|
271
|
+ if req.request_type == control::RequestType::Standard
|
|
247
|
272
|
&& req.recipient == control::Recipient::Interface
|
|
248
|
|
- && req.index == u8::from(self.audio_control) as u16)
|
|
|
273
|
+ && req.request == control::Request::SET_INTERFACE
|
|
|
274
|
+ {
|
|
|
275
|
+ // TODO
|
|
|
276
|
+ xfer.accept().ok();
|
|
|
277
|
+ return;
|
|
|
278
|
+ }
|
|
|
279
|
+
|
|
|
280
|
+ // We only support interface requests.
|
|
|
281
|
+ if req.request_type != control::RequestType::Class
|
|
|
282
|
+ || req.recipient != control::Recipient::Interface
|
|
249
|
283
|
{
|
|
250
|
284
|
return;
|
|
251
|
285
|
}
|
|
252
|
286
|
|
|
|
287
|
+ let interface = req.index & 0xff;
|
|
|
288
|
+ let entity = req.index >> 8;
|
|
|
289
|
+
|
|
|
290
|
+ if interface != u8::from(self.audio_control) as u16 {
|
|
|
291
|
+ return;
|
|
|
292
|
+ }
|
|
|
293
|
+
|
|
|
294
|
+ if entity != 0x2 {
|
|
|
295
|
+ xfer.reject().ok();
|
|
|
296
|
+ return;
|
|
|
297
|
+ }
|
|
|
298
|
+
|
|
|
299
|
+ /*if !(req.request_type == control::RequestType::Class
|
|
|
300
|
+ && req.recipient == control::Recipient::Interface
|
|
|
301
|
+ && req.index == u8::from(self.audio_control) as u16)
|
|
|
302
|
+ {
|
|
|
303
|
+ return;
|
|
|
304
|
+ }*/
|
|
|
305
|
+
|
|
253
|
306
|
match req.request {
|
|
254
|
|
- // TODO
|
|
|
307
|
+ SET_CUR => {
|
|
|
308
|
+ let control_selector = (req.value >> 8) as u8;
|
|
|
309
|
+ let channel_number = req.value & 0xff;
|
|
|
310
|
+
|
|
|
311
|
+ match control_selector {
|
|
|
312
|
+ MUTE_CONTROL if xfer.data().len() == 1 => {
|
|
|
313
|
+ // TODO: Channel?
|
|
|
314
|
+ self.mute = xfer.data()[0];
|
|
|
315
|
+ xfer.accept().ok();
|
|
|
316
|
+ }
|
|
|
317
|
+ VOLUME_CONTROL if xfer.data().len() == 2 => {
|
|
|
318
|
+ // TODO: Channel?
|
|
|
319
|
+ self.volume[0] = xfer.data()[0];
|
|
|
320
|
+ self.volume[1] = xfer.data()[1];
|
|
|
321
|
+ xfer.accept().ok();
|
|
|
322
|
+ }
|
|
|
323
|
+ _ => {
|
|
|
324
|
+ xfer.reject().ok();
|
|
|
325
|
+ }
|
|
|
326
|
+ }
|
|
|
327
|
+ }
|
|
255
|
328
|
_ => {
|
|
256
|
329
|
xfer.reject().ok();
|
|
257
|
330
|
}
|
|
|
@@ -261,14 +334,156 @@ impl<B: UsbBus, OUTBUF: AsRef<[u32]>> UsbClass<B> for UsbAudioClass<'_, B, OUTBU
|
|
261
|
334
|
fn control_in(&mut self, xfer: ControlIn<B>) {
|
|
262
|
335
|
let req = xfer.request();
|
|
263
|
336
|
|
|
264
|
|
- if !(req.request_type == control::RequestType::Class
|
|
|
337
|
+ // We only support interface requests.
|
|
|
338
|
+ if req.request_type != control::RequestType::Class
|
|
|
339
|
+ || req.recipient != control::Recipient::Interface
|
|
|
340
|
+ {
|
|
|
341
|
+ return;
|
|
|
342
|
+ }
|
|
|
343
|
+
|
|
|
344
|
+ let interface = req.index & 0xff;
|
|
|
345
|
+ let entity = req.index >> 8;
|
|
|
346
|
+
|
|
|
347
|
+ if interface != u8::from(self.audio_control) as u16 {
|
|
|
348
|
+ return;
|
|
|
349
|
+ }
|
|
|
350
|
+
|
|
|
351
|
+ if entity != 0x2 {
|
|
|
352
|
+ xfer.reject().ok();
|
|
|
353
|
+ return;
|
|
|
354
|
+ }
|
|
|
355
|
+
|
|
|
356
|
+ /*if !(req.request_type == control::RequestType::Class
|
|
265
|
357
|
&& req.recipient == control::Recipient::Interface
|
|
266
|
358
|
&& req.index == u8::from(self.audio_control) as u16)
|
|
267
|
359
|
{
|
|
268
|
360
|
return;
|
|
269
|
|
- }
|
|
|
361
|
+ }*/
|
|
270
|
362
|
|
|
271
|
363
|
match req.request {
|
|
|
364
|
+ GET_MIN => {
|
|
|
365
|
+ let control_selector = (req.value >> 8) as u8;
|
|
|
366
|
+ let channel_number = req.value & 0xff;
|
|
|
367
|
+
|
|
|
368
|
+ match control_selector {
|
|
|
369
|
+ MUTE_CONTROL => {
|
|
|
370
|
+ if channel_number == 0xff && req.length == 2 {
|
|
|
371
|
+ // TODO
|
|
|
372
|
+ xfer.accept_with(&[0, 0]).ok();
|
|
|
373
|
+ } else if channel_number != 0xff && req.length == 1 {
|
|
|
374
|
+ // TODO
|
|
|
375
|
+ xfer.accept_with(&[0]).ok();
|
|
|
376
|
+ } else {
|
|
|
377
|
+ xfer.reject().ok();
|
|
|
378
|
+ }
|
|
|
379
|
+ }
|
|
|
380
|
+ VOLUME_CONTROL => {
|
|
|
381
|
+ if channel_number == 0xff && req.length == 4 {
|
|
|
382
|
+ // TODO
|
|
|
383
|
+ xfer.accept_with(&[0x00, 0x00, 0x00, 0x00]).ok();
|
|
|
384
|
+ } else if channel_number != 0xff && req.length == 2 {
|
|
|
385
|
+ // TODO
|
|
|
386
|
+ xfer.accept_with(&[0x00, 0x00]).ok();
|
|
|
387
|
+ } else {
|
|
|
388
|
+ xfer.reject().ok();
|
|
|
389
|
+ }
|
|
|
390
|
+ }
|
|
|
391
|
+ _ => {
|
|
|
392
|
+ xfer.reject().ok();
|
|
|
393
|
+ }
|
|
|
394
|
+ }
|
|
|
395
|
+ }
|
|
|
396
|
+ GET_MAX => {
|
|
|
397
|
+ let control_selector = (req.value >> 8) as u8;
|
|
|
398
|
+ let channel_number = req.value & 0xff;
|
|
|
399
|
+
|
|
|
400
|
+ match control_selector {
|
|
|
401
|
+ MUTE_CONTROL => {
|
|
|
402
|
+ if channel_number == 0xff && req.length == 2 {
|
|
|
403
|
+ // TODO
|
|
|
404
|
+ xfer.accept_with(&[1, 1]).ok();
|
|
|
405
|
+ } else if channel_number != 0xff && req.length == 1 {
|
|
|
406
|
+ // TODO
|
|
|
407
|
+ xfer.accept_with(&[1]).ok();
|
|
|
408
|
+ } else {
|
|
|
409
|
+ xfer.reject().ok();
|
|
|
410
|
+ }
|
|
|
411
|
+ }
|
|
|
412
|
+ VOLUME_CONTROL => {
|
|
|
413
|
+ if channel_number == 0xff && req.length == 4 {
|
|
|
414
|
+ // TODO
|
|
|
415
|
+ xfer.accept_with(&[0x00, 0x01, 0x00, 0x01]).ok();
|
|
|
416
|
+ } else if channel_number != 0xff && req.length == 2 {
|
|
|
417
|
+ // TODO
|
|
|
418
|
+ xfer.accept_with(&[0x00, 0x01]).ok();
|
|
|
419
|
+ } else {
|
|
|
420
|
+ xfer.reject().ok();
|
|
|
421
|
+ }
|
|
|
422
|
+ }
|
|
|
423
|
+ _ => {
|
|
|
424
|
+ xfer.reject().ok();
|
|
|
425
|
+ }
|
|
|
426
|
+ }
|
|
|
427
|
+ }
|
|
|
428
|
+ GET_RES => {
|
|
|
429
|
+ let control_selector = (req.value >> 8) as u8;
|
|
|
430
|
+ let channel_number = req.value & 0xff;
|
|
|
431
|
+
|
|
|
432
|
+ match control_selector {
|
|
|
433
|
+ VOLUME_CONTROL => {
|
|
|
434
|
+ if channel_number == 0xff && req.length == 4 {
|
|
|
435
|
+ // TODO
|
|
|
436
|
+ xfer.accept_with(&[0x01, 0x00, 0x01, 0x00]).ok();
|
|
|
437
|
+ } else if channel_number != 0xff && req.length == 2 {
|
|
|
438
|
+ // TODO
|
|
|
439
|
+ xfer.accept_with(&[0x01, 0x00]).ok();
|
|
|
440
|
+ } else {
|
|
|
441
|
+ xfer.reject().ok();
|
|
|
442
|
+ }
|
|
|
443
|
+ }
|
|
|
444
|
+ _ => {
|
|
|
445
|
+ xfer.reject().ok();
|
|
|
446
|
+ }
|
|
|
447
|
+ }
|
|
|
448
|
+ }
|
|
|
449
|
+ GET_CUR => {
|
|
|
450
|
+ let control_selector = (req.value >> 8) as u8;
|
|
|
451
|
+ let channel_number = req.value & 0xff;
|
|
|
452
|
+
|
|
|
453
|
+ match control_selector {
|
|
|
454
|
+ MUTE_CONTROL => {
|
|
|
455
|
+ if channel_number == 0xff && req.length == 2 {
|
|
|
456
|
+ // TODO
|
|
|
457
|
+ xfer.accept_with(&[self.mute, self.mute]).ok();
|
|
|
458
|
+ } else if channel_number != 0xff && req.length == 1 {
|
|
|
459
|
+ // TODO
|
|
|
460
|
+ xfer.accept_with(&[self.mute]).ok();
|
|
|
461
|
+ } else {
|
|
|
462
|
+ xfer.reject().ok();
|
|
|
463
|
+ }
|
|
|
464
|
+ }
|
|
|
465
|
+ VOLUME_CONTROL => {
|
|
|
466
|
+ if channel_number == 0xff && req.length == 4 {
|
|
|
467
|
+ // TODO
|
|
|
468
|
+ xfer.accept_with(&[
|
|
|
469
|
+ self.volume[0],
|
|
|
470
|
+ self.volume[1],
|
|
|
471
|
+ self.volume[0],
|
|
|
472
|
+ self.volume[1],
|
|
|
473
|
+ ])
|
|
|
474
|
+ .ok();
|
|
|
475
|
+ } else if channel_number != 0xff && req.length == 2 {
|
|
|
476
|
+ // TODO
|
|
|
477
|
+ xfer.accept_with(&self.volume).ok();
|
|
|
478
|
+ } else {
|
|
|
479
|
+ xfer.reject().ok();
|
|
|
480
|
+ }
|
|
|
481
|
+ }
|
|
|
482
|
+ _ => {
|
|
|
483
|
+ xfer.reject().ok();
|
|
|
484
|
+ }
|
|
|
485
|
+ }
|
|
|
486
|
+ }
|
|
272
|
487
|
// TODO
|
|
273
|
488
|
_ => {
|
|
274
|
489
|
xfer.reject().ok();
|
|
|
@@ -276,7 +491,7 @@ impl<B: UsbBus, OUTBUF: AsRef<[u32]>> UsbClass<B> for UsbAudioClass<'_, B, OUTBU
|
|
276
|
491
|
};
|
|
277
|
492
|
}
|
|
278
|
493
|
|
|
279
|
|
- /*fn endpoint_out(&mut self, addr: EndpointAddress) {
|
|
|
494
|
+ fn endpoint_out(&mut self, addr: EndpointAddress) {
|
|
280
|
495
|
if addr == self.audio_out.address() {
|
|
281
|
496
|
if self.audio_out_buf.is_some() {
|
|
282
|
497
|
// TODO: Write data into buffer, move buffer somewhere else.
|
|
|
@@ -285,292 +500,14 @@ impl<B: UsbBus, OUTBUF: AsRef<[u32]>> UsbClass<B> for UsbAudioClass<'_, B, OUTBU
|
|
285
|
500
|
}
|
|
286
|
501
|
// TODO: Process incoming audio data.
|
|
287
|
502
|
let mut buffer = [0u8; 256];
|
|
288
|
|
- self.audio_out.read(&mut buffer);
|
|
|
503
|
+ self.audio_out.read(&mut buffer).ok();
|
|
289
|
504
|
}
|
|
290
|
505
|
}
|
|
291
|
506
|
|
|
292
|
507
|
fn endpoint_in_complete(&mut self, addr: EndpointAddress) {
|
|
293
|
|
- if addr == self.audio_out_sync.address() {
|
|
|
508
|
+ /*if addr == self.audio_out_sync.address() {
|
|
294
|
509
|
// Immediately write the next sync value.
|
|
295
|
510
|
//self.audio_out_sync.write(&self.samples_per_frame).unwrap();
|
|
296
|
|
- }
|
|
297
|
|
- }*/
|
|
298
|
|
-}
|
|
299
|
|
-
|
|
300
|
|
-/*use usb_device::class_prelude::*;
|
|
301
|
|
-use usb_device::endpoint::{IsochronousSynchronizationType, IsochronousUsageType};
|
|
302
|
|
-use usb_device::Result;
|
|
303
|
|
-
|
|
304
|
|
-/// This should be used as `device_class` when building the `UsbDevice`.
|
|
305
|
|
-pub const USB_INTERFACE_CLASS_AUDIO: u8 = 0x01;
|
|
306
|
|
-
|
|
307
|
|
-const USB_AUDIO_SUBCLASS_UNDEFINED: u8 = 0x0;
|
|
308
|
|
-const USB_AUDIO_SUBCLASS_AUDIOCONTROL: u8 = 0x01;
|
|
309
|
|
-const USB_AUDIO_SUBCLASS_AUDIOSTREAMING: u8 = 0x02;
|
|
310
|
|
-const USB_AUDIO_SUBCLASS_MIDISTREAMING: u8 = 0x03;
|
|
311
|
|
-
|
|
312
|
|
-const USB_CLASS_CDC_DATA: u8 = 0x0a;
|
|
313
|
|
-const CDC_SUBCLASS_ACM: u8 = 0x02;
|
|
314
|
|
-const USB_AUDIO_PROTOCOL_NONE: u8 = 0x00;
|
|
315
|
|
-
|
|
316
|
|
-const CS_DEVICE: u8 = 0x21;
|
|
317
|
|
-const CS_CONFIGURATION: u8 = 0x22;
|
|
318
|
|
-const CS_STRING: u8 = 0x23;
|
|
319
|
|
-const CS_INTERFACE: u8 = 0x24;
|
|
320
|
|
-const CS_ENDPOINT: u8 = 0x25;
|
|
321
|
|
-
|
|
322
|
|
-const EP_GENERAL: u8 = 0x1;
|
|
323
|
|
-
|
|
324
|
|
-const AC_DESC_TYPE_HEADER: u8 = 0x1;
|
|
325
|
|
-const AC_DESC_TYPE_INPUT_TERMINAL: u8 = 0x2;
|
|
326
|
|
-const AC_DESC_TYPE_OUTPUT_TERMINAL: u8 = 0x3;
|
|
327
|
|
-const AC_DESC_TYPE_MIXER_UNIT: u8 = 0x4;
|
|
328
|
|
-const AC_DESC_TYPE_SELECTOR_UNIT: u8 = 0x5;
|
|
329
|
|
-const AC_DESC_TYPE_FEATURE_UNIT: u8 = 0x6;
|
|
330
|
|
-const AC_DESC_TYPE_PROCESSING_UNIT: u8 = 0x7;
|
|
331
|
|
-const AC_DESC_TYPE_EXTENSION_UNIT: u8 = 0x8;
|
|
332
|
|
-
|
|
333
|
|
-const AC_DESC_ST_GENERAL: u8 = 0x1;
|
|
334
|
|
-const AC_DESC_ST_FORMAT_TYPE: u8 = 0x2;
|
|
335
|
|
-const AC_DESC_ST_FORMAT_SPECIFIC: u8 = 0x3;
|
|
336
|
|
-
|
|
337
|
|
-const AUDIO_SUB_TYPE_HEADER: u8 = 0x01;
|
|
338
|
|
-const AUDIO_SUB_TYPE_INPUT_TERMINAL: u8 = 0x02;
|
|
339
|
|
-const AUDIO_SUB_TYPE_FEATURE_UNIT: u8 = 0x06;
|
|
340
|
|
-const AUDIO_SUB_TYPE_OUTPUT_TERMINAL: u8 = 0x03;
|
|
341
|
|
-const AUDIO_SUB_TYPE_AS_GENERAL: u8 = 0x01;
|
|
342
|
|
-const AUDIO_SUB_TYPE_FORMAT_TYPE: u8 = 0x02;
|
|
343
|
|
-
|
|
344
|
|
-pub struct UsbAudioClass<'a, B: UsbBus> {
|
|
345
|
|
- audio_control_if: InterfaceNumber,
|
|
346
|
|
- audio_stream: InterfaceNumber,
|
|
347
|
|
- alt_audio_stream: InterfaceNumber,
|
|
348
|
|
- audio_in_ep: EndpointIn<'a, B>,
|
|
349
|
|
-}
|
|
350
|
|
-
|
|
351
|
|
-impl<B: UsbBus> UsbAudioClass<'_, B> {
|
|
352
|
|
- /// Creates a new UsbAudioClass with the provided UsbBus and max_packet_size in bytes. For
|
|
353
|
|
- /// full-speed devices, max_packet_size has to be one of 8, 16, 32 or 64.
|
|
354
|
|
- pub fn new(alloc: &UsbBusAllocator<B>, max_packet_size: u16) -> UsbAudioClass<'_, B> {
|
|
355
|
|
- UsbAudioClass {
|
|
356
|
|
- audio_control_if: alloc.interface(),
|
|
357
|
|
- audio_stream: alloc.interface(),
|
|
358
|
|
- alt_audio_stream: alloc.interface(),
|
|
359
|
|
- audio_in_ep: alloc.isochronous(
|
|
360
|
|
- IsochronousSynchronizationType::Synchronous,
|
|
361
|
|
- IsochronousUsageType::Data,
|
|
362
|
|
- max_packet_size,
|
|
363
|
|
- 4,
|
|
364
|
|
- ),
|
|
365
|
|
- }
|
|
366
|
|
- }
|
|
367
|
|
-
|
|
368
|
|
- /// Gets the maximum packet size in bytes.
|
|
369
|
|
- pub fn max_packet_size(&self) -> u16 {
|
|
370
|
|
- self.audio_in_ep.max_packet_size()
|
|
371
|
|
- }
|
|
372
|
|
-
|
|
373
|
|
- /// Writes a single packet into the IN endpoint.
|
|
374
|
|
- pub fn write_packet(&mut self, data: &[u8]) -> Result<usize> {
|
|
375
|
|
- //defmt::info!("write_packet");
|
|
376
|
|
- self.audio_in_ep.write(data)
|
|
377
|
|
- }
|
|
378
|
|
-
|
|
379
|
|
- /// Gets the address of the IN endpoint.
|
|
380
|
|
- pub(crate) fn write_ep_address(&self) -> EndpointAddress {
|
|
381
|
|
- self.audio_in_ep.address()
|
|
|
511
|
+ }*/
|
|
382
|
512
|
}
|
|
383
|
513
|
}
|
|
384
|
|
-
|
|
385
|
|
-impl<B: UsbBus> UsbClass<B> for UsbAudioClass<'_, B> {
|
|
386
|
|
- fn get_configuration_descriptors(&self, writer: &mut DescriptorWriter) -> Result<()> {
|
|
387
|
|
- writer.iad(
|
|
388
|
|
- self.audio_control_if,
|
|
389
|
|
- 3,
|
|
390
|
|
- 0x0, // device defined at interface level
|
|
391
|
|
- 0x0, // subclass unused
|
|
392
|
|
- USB_AUDIO_PROTOCOL_NONE,
|
|
393
|
|
- )?;
|
|
394
|
|
-
|
|
395
|
|
- writer.interface(
|
|
396
|
|
- self.audio_control_if,
|
|
397
|
|
- USB_INTERFACE_CLASS_AUDIO,
|
|
398
|
|
- USB_AUDIO_SUBCLASS_AUDIOCONTROL,
|
|
399
|
|
- USB_AUDIO_PROTOCOL_NONE,
|
|
400
|
|
- )?;
|
|
401
|
|
-
|
|
402
|
|
- writer.write(
|
|
403
|
|
- CS_INTERFACE,
|
|
404
|
|
- &[
|
|
405
|
|
- AUDIO_SUB_TYPE_HEADER, // bDescriptorSubtype
|
|
406
|
|
- 0x00,
|
|
407
|
|
- 0x1, // bcdADC (1.0),
|
|
408
|
|
- 43,
|
|
409
|
|
- 0, // wTotalLength (Compute this!)
|
|
410
|
|
- 0x01, // bInCollection (1 streaming interface)
|
|
411
|
|
- 0x01, // baInterfaceNr (Interface 1 is stream)
|
|
412
|
|
- ],
|
|
413
|
|
- )?;
|
|
414
|
|
-
|
|
415
|
|
- writer.write(
|
|
416
|
|
- CS_INTERFACE,
|
|
417
|
|
- &[
|
|
418
|
|
- AUDIO_SUB_TYPE_INPUT_TERMINAL, // bDescriptorSubtype
|
|
419
|
|
- 0x01, // bTerminalID 1,
|
|
420
|
|
- 0x10,
|
|
421
|
|
- 0x07, // wTerminalType (radio receiver)
|
|
422
|
|
- 0x00, // bAssocTerminal (none)
|
|
423
|
|
- 0x02, // bNrChannels - 2 for I and Q
|
|
424
|
|
- 0x03,
|
|
425
|
|
- 0x00, // wChannelConfig (left, right)
|
|
426
|
|
- 0x00, // iChannelNames (none)
|
|
427
|
|
- 0x00, // iTerminal (none)
|
|
428
|
|
- ],
|
|
429
|
|
- )?;
|
|
430
|
|
-
|
|
431
|
|
- writer.write(
|
|
432
|
|
- CS_INTERFACE,
|
|
433
|
|
- &[
|
|
434
|
|
- AUDIO_SUB_TYPE_FEATURE_UNIT, // bDescriptorSubtype
|
|
435
|
|
- 0x02, // bUnitID,
|
|
436
|
|
- 0x01, // bSourceID (input terminal 1)
|
|
437
|
|
- 0x02, // bControlSize (2 bytes)
|
|
438
|
|
- 0x01,
|
|
439
|
|
- 0x00, // Master controls
|
|
440
|
|
- 0x00,
|
|
441
|
|
- 0x00, // Channel 0 controls
|
|
442
|
|
- 0x00,
|
|
443
|
|
- 0x00, // Channel 1 controls
|
|
444
|
|
- 0x00, // iFeature (none)
|
|
445
|
|
- ],
|
|
446
|
|
- )?;
|
|
447
|
|
-
|
|
448
|
|
- writer.write(
|
|
449
|
|
- CS_INTERFACE,
|
|
450
|
|
- &[
|
|
451
|
|
- AUDIO_SUB_TYPE_OUTPUT_TERMINAL, // bDescriptorSubtype
|
|
452
|
|
- 0x03, // bTerminalID,
|
|
453
|
|
- 0x01,
|
|
454
|
|
- 0x01, // wTerminalType (USB Streaming)
|
|
455
|
|
- 0x00, // bAssocTerminal (none)
|
|
456
|
|
- 0x02, // bSourceID (feature unit 2)
|
|
457
|
|
- 0x00, // iTerminal (none)
|
|
458
|
|
- ],
|
|
459
|
|
- )?;
|
|
460
|
|
-
|
|
461
|
|
- writer.interface(
|
|
462
|
|
- self.audio_stream,
|
|
463
|
|
- USB_INTERFACE_CLASS_AUDIO,
|
|
464
|
|
- USB_AUDIO_SUBCLASS_AUDIOSTREAMING,
|
|
465
|
|
- USB_AUDIO_PROTOCOL_NONE,
|
|
466
|
|
- )?;
|
|
467
|
|
-
|
|
468
|
|
- //alternate audio stream
|
|
469
|
|
- /*writer.interface_alt(
|
|
470
|
|
- self.alt_audio_stream,
|
|
471
|
|
- 1,
|
|
472
|
|
- USB_INTERFACE_CLASS_AUDIO,
|
|
473
|
|
- USB_AUDIO_SUBCLASS_AUDIOSTREAMING,
|
|
474
|
|
- USB_AUDIO_PROTOCOL_NONE,
|
|
475
|
|
- None,
|
|
476
|
|
- )?;*/
|
|
477
|
|
-
|
|
478
|
|
- // Audio Stream Audio Class Descriptor
|
|
479
|
|
- writer.write(
|
|
480
|
|
- CS_INTERFACE,
|
|
481
|
|
- &[
|
|
482
|
|
- AUDIO_SUB_TYPE_AS_GENERAL, // bDescriptorSubtype
|
|
483
|
|
- 0x03, // bTerminalID,
|
|
484
|
|
- 0x00, // bDelay
|
|
485
|
|
- 0x01,
|
|
486
|
|
- 0x00, // wFormatTag (PCM Format)
|
|
487
|
|
- ],
|
|
488
|
|
- )?;
|
|
489
|
|
-
|
|
490
|
|
- // Format Type Audio Descriptor
|
|
491
|
|
- writer.write(
|
|
492
|
|
- CS_INTERFACE,
|
|
493
|
|
- &[
|
|
494
|
|
- AUDIO_SUB_TYPE_FORMAT_TYPE, // bDescriptorSubtype
|
|
495
|
|
- 0x01, // bFormatType (TYPE_I)
|
|
496
|
|
- 0x02, // bNrChannels (2)
|
|
497
|
|
- 0x02, // bSubFrameSize (2)
|
|
498
|
|
- 0x10, // bBitResolution (16 bits)
|
|
499
|
|
- 0x01, // bSamFreqType (1 sample frequency)
|
|
500
|
|
- 0x80, // 8*2 = 16 KHz byte 0
|
|
501
|
|
- 0x3E, // 8*2 = 16 KHz byte 1
|
|
502
|
|
- 0x00, // 8*2 = 16 KHz byte 2
|
|
503
|
|
- ],
|
|
504
|
|
- )?;
|
|
505
|
|
-
|
|
506
|
|
- // TODO: Set the necessary flags for Isochronous
|
|
507
|
|
- writer.endpoint(&self.audio_in_ep)?;
|
|
508
|
|
-
|
|
509
|
|
- // Isochronous endpoint Audio Class descriptor
|
|
510
|
|
- writer.write(
|
|
511
|
|
- CS_ENDPOINT,
|
|
512
|
|
- &[
|
|
513
|
|
- EP_GENERAL, // bDescriptorSubtype
|
|
514
|
|
- 0x00, // bmAttributes (none)
|
|
515
|
|
- 0x02, // bLockDelayUnits (PCM Samples)
|
|
516
|
|
- 0x00, 0x00, // wLockDelay (0) - should be zero for asynchronous
|
|
517
|
|
- ],
|
|
518
|
|
- )?;
|
|
519
|
|
- Ok(())
|
|
520
|
|
- }
|
|
521
|
|
-
|
|
522
|
|
- fn reset(&mut self) {}
|
|
523
|
|
-
|
|
524
|
|
- fn control_in(&mut self, xfer: ControlIn<B>) {
|
|
525
|
|
- let req = xfer.request();
|
|
526
|
|
-
|
|
527
|
|
- if !(req.request_type == control::RequestType::Class
|
|
528
|
|
- && req.recipient == control::Recipient::Interface
|
|
529
|
|
- && req.index == u8::from(self.audio_control_if) as u16)
|
|
530
|
|
- {
|
|
531
|
|
- return;
|
|
532
|
|
- }
|
|
533
|
|
-
|
|
534
|
|
- //defmt::info!("control_in - req : {:?}", req.request);
|
|
535
|
|
- match req.request {
|
|
536
|
|
- /*
|
|
537
|
|
- REQ_GET_LINE_CODING if req.length == 7 => {
|
|
538
|
|
- xfer.accept(|data| {
|
|
539
|
|
- data[0..4].copy_from_slice(&self.line_coding.data_rate.to_le_bytes());
|
|
540
|
|
- data[4] = self.line_coding.stop_bits as u8;
|
|
541
|
|
- data[5] = self.line_coding.parity_type as u8;
|
|
542
|
|
- data[6] = self.line_coding.data_bits;
|
|
543
|
|
-
|
|
544
|
|
- Ok(7)
|
|
545
|
|
- }).ok();
|
|
546
|
|
- },
|
|
547
|
|
- */
|
|
548
|
|
- _ => {
|
|
549
|
|
- xfer.reject().ok();
|
|
550
|
|
- }
|
|
551
|
|
- }
|
|
552
|
|
- }
|
|
553
|
|
-
|
|
554
|
|
- fn control_out(&mut self, xfer: ControlOut<B>) {
|
|
555
|
|
- let req = xfer.request();
|
|
556
|
|
-
|
|
557
|
|
- if !(req.request_type == control::RequestType::Class
|
|
558
|
|
- && req.recipient == control::Recipient::Interface
|
|
559
|
|
- && req.index == u8::from(self.audio_control_if) as u16)
|
|
560
|
|
- {
|
|
561
|
|
- return;
|
|
562
|
|
- }
|
|
563
|
|
-
|
|
564
|
|
- match req.request {
|
|
565
|
|
- /*
|
|
566
|
|
- REQ_SEND_ENCAPSULATED_COMMAND => {
|
|
567
|
|
- // We don't actually support encapsulated commands but pretend we do for standards
|
|
568
|
|
- // compatibility.
|
|
569
|
|
- xfer.accept().ok();
|
|
570
|
|
- },*/
|
|
571
|
|
- _ => {
|
|
572
|
|
- xfer.reject().ok();
|
|
573
|
|
- }
|
|
574
|
|
- };
|
|
575
|
|
- }
|
|
576
|
|
-}*/
|