1
- use crate :: bus:: { PollResult , StringIndex , UsbBus , UsbBusAllocator } ;
1
+ use crate :: bus:: { InterfaceNumber , PollResult , StringIndex , UsbBus , UsbBusAllocator } ;
2
2
use crate :: class:: { ControlIn , ControlOut , UsbClass } ;
3
3
use crate :: control;
4
4
use crate :: control_pipe:: ControlPipe ;
@@ -338,7 +338,25 @@ impl<B: UsbBus> UsbDevice<'_, B> {
338
338
}
339
339
340
340
( Recipient :: Interface , Request :: GET_INTERFACE ) => {
341
- // TODO: change when alternate settings are implemented
341
+ // Reject interface numbers bigger than 255
342
+ if req. index > core:: u8:: MAX . into ( ) {
343
+ xfer. reject ( ) . ok ( ) ;
344
+ return ;
345
+ }
346
+
347
+ // Ask class implementations, whether they know the alternate setting
348
+ // of the interface in question
349
+ for cls in classes {
350
+ match cls. get_alt_setting ( InterfaceNumber ( req. index as u8 ) ) {
351
+ Some ( setting) => {
352
+ xfer. accept_with ( & setting. to_le_bytes ( ) ) . ok ( ) ;
353
+ return ;
354
+ }
355
+ None => ( ) ,
356
+ }
357
+ }
358
+
359
+ // If no class returned an alternate setting, return the default value
342
360
xfer. accept_with ( & DEFAULT_ALTERNATE_SETTING . to_le_bytes ( ) )
343
361
. ok ( ) ;
344
362
}
@@ -355,7 +373,7 @@ impl<B: UsbBus> UsbDevice<'_, B> {
355
373
fn control_out ( & mut self , classes : & mut ClassList < ' _ , B > , req : control:: Request ) {
356
374
use crate :: control:: { Recipient , Request } ;
357
375
358
- for cls in classes {
376
+ for cls in classes. iter_mut ( ) {
359
377
cls. control_out ( ControlOut :: new ( & mut self . control , & req) ) ;
360
378
361
379
if !self . control . waiting_for_response ( ) {
@@ -428,9 +446,28 @@ impl<B: UsbBus> UsbDevice<'_, B> {
428
446
}
429
447
}
430
448
431
- ( Recipient :: Interface , Request :: SET_INTERFACE , DEFAULT_ALTERNATE_SETTING_U16 ) => {
432
- // TODO: do something when alternate settings are implemented
433
- xfer. accept ( ) . ok ( ) ;
449
+ ( Recipient :: Interface , Request :: SET_INTERFACE , alt_setting) => {
450
+ // Reject interface numbers and alt settings bigger than 255
451
+ if req. index > core:: u8:: MAX . into ( ) || alt_setting > core:: u8:: MAX . into ( ) {
452
+ xfer. reject ( ) . ok ( ) ;
453
+ return ;
454
+ }
455
+
456
+ // Ask class implementations, whether they accept the alternate interface setting.
457
+ for cls in classes {
458
+ if cls. set_alt_setting ( InterfaceNumber ( req. index as u8 ) , alt_setting as u8 )
459
+ {
460
+ xfer. accept ( ) . ok ( ) ;
461
+ return ;
462
+ }
463
+ }
464
+
465
+ // Default behaviour, if no class implementation accepted the alternate setting.
466
+ if alt_setting == DEFAULT_ALTERNATE_SETTING_U16 {
467
+ xfer. accept ( ) . ok ( ) ;
468
+ } else {
469
+ xfer. reject ( ) . ok ( ) ;
470
+ }
434
471
}
435
472
436
473
_ => {
0 commit comments