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 ;
@@ -357,7 +357,25 @@ impl<B: UsbBus> UsbDevice<'_, B> {
357
357
}
358
358
359
359
( Recipient :: Interface , Request :: GET_INTERFACE ) => {
360
- // TODO: change when alternate settings are implemented
360
+ // Reject interface numbers bigger than 255
361
+ if req. index > core:: u8:: MAX . into ( ) {
362
+ xfer. reject ( ) . ok ( ) ;
363
+ return ;
364
+ }
365
+
366
+ // Ask class implementations, whether they know the alternate setting
367
+ // of the interface in question
368
+ for cls in classes {
369
+ match cls. get_alt_setting ( InterfaceNumber ( req. index as u8 ) ) {
370
+ Some ( setting) => {
371
+ xfer. accept_with ( & setting. to_le_bytes ( ) ) . ok ( ) ;
372
+ return ;
373
+ }
374
+ None => ( ) ,
375
+ }
376
+ }
377
+
378
+ // If no class returned an alternate setting, return the default value
361
379
xfer. accept_with ( & DEFAULT_ALTERNATE_SETTING . to_le_bytes ( ) )
362
380
. ok ( ) ;
363
381
}
@@ -374,7 +392,7 @@ impl<B: UsbBus> UsbDevice<'_, B> {
374
392
fn control_out ( & mut self , classes : & mut ClassList < ' _ , B > , req : control:: Request ) {
375
393
use crate :: control:: { Recipient , Request } ;
376
394
377
- for cls in classes {
395
+ for cls in classes. iter_mut ( ) {
378
396
cls. control_out ( ControlOut :: new ( & mut self . control , & req) ) ;
379
397
380
398
if !self . control . waiting_for_response ( ) {
@@ -447,9 +465,28 @@ impl<B: UsbBus> UsbDevice<'_, B> {
447
465
}
448
466
}
449
467
450
- ( Recipient :: Interface , Request :: SET_INTERFACE , DEFAULT_ALTERNATE_SETTING_U16 ) => {
451
- // TODO: do something when alternate settings are implemented
452
- xfer. accept ( ) . ok ( ) ;
468
+ ( Recipient :: Interface , Request :: SET_INTERFACE , alt_setting) => {
469
+ // Reject interface numbers and alt settings bigger than 255
470
+ if req. index > core:: u8:: MAX . into ( ) || alt_setting > core:: u8:: MAX . into ( ) {
471
+ xfer. reject ( ) . ok ( ) ;
472
+ return ;
473
+ }
474
+
475
+ // Ask class implementations, whether they accept the alternate interface setting.
476
+ for cls in classes {
477
+ if cls. set_alt_setting ( InterfaceNumber ( req. index as u8 ) , alt_setting as u8 )
478
+ {
479
+ xfer. accept ( ) . ok ( ) ;
480
+ return ;
481
+ }
482
+ }
483
+
484
+ // Default behaviour, if no class implementation accepted the alternate setting.
485
+ if alt_setting == DEFAULT_ALTERNATE_SETTING_U16 {
486
+ xfer. accept ( ) . ok ( ) ;
487
+ } else {
488
+ xfer. reject ( ) . ok ( ) ;
489
+ }
453
490
}
454
491
455
492
_ => {
0 commit comments