@@ -69,28 +69,30 @@ impl<B: UsbBus> ControlPipe<'_, B> {
69
69
70
70
pub fn handle_setup ( & mut self ) -> Option < Request > {
71
71
let count = match self . ep_out . read ( & mut self . buf [ ..] ) {
72
- Ok ( count) => count,
72
+ Ok ( count) => {
73
+ usb_trace ! ( "Read {count} bytes on EP0-OUT: {:?}" , & self . buf[ ..count] ) ;
74
+ count
75
+ }
73
76
Err ( UsbError :: WouldBlock ) => return None ,
74
77
Err ( _) => {
75
- self . set_error ( ) ;
76
78
return None ;
77
79
}
78
80
} ;
79
81
80
82
let req = match Request :: parse ( & self . buf [ 0 ..count] ) {
81
83
Ok ( req) => req,
82
84
Err ( _) => {
83
- // Failed to parse SETUP packet
84
- self . set_error ( ) ;
85
+ // Failed to parse SETUP packet. We are supposed to silently ignore this.
85
86
return None ;
86
87
}
87
88
} ;
88
89
89
90
// Now that we have properly parsed the setup packet, ensure the end-point is no longer in
90
91
// a stalled state.
91
- usb_trace ! ( "EP0 request received: {req:?}" ) ;
92
92
self . ep_out . unstall ( ) ;
93
93
94
+ usb_debug ! ( "EP0 request received: {req:?}" ) ;
95
+
94
96
/*sprintln!("SETUP {:?} {:?} {:?} req:{} val:{} idx:{} len:{} {:?}",
95
97
req.direction, req.request_type, req.recipient,
96
98
req.request, req.value, req.index, req.length,
@@ -104,7 +106,6 @@ impl<B: UsbBus> ControlPipe<'_, B> {
104
106
105
107
if req. length as usize > self . buf . len ( ) {
106
108
// Data stage won't fit in buffer
107
- self . set_error ( ) ;
108
109
return None ;
109
110
}
110
111
@@ -143,11 +144,11 @@ impl<B: UsbBus> ControlPipe<'_, B> {
143
144
}
144
145
} ;
145
146
147
+ usb_trace ! ( "Read {count} bytes on EP0-OUT: {:?}" , & self . buf[ i..( i + count) ] ) ;
146
148
self . i += count;
147
- usb_trace ! ( "Read {count} bytes on EP0-OUT" ) ;
148
149
149
150
if self . i >= self . len {
150
- usb_debug ! ( "Request OUT complete: {req}" ) ;
151
+ usb_debug ! ( "Request OUT complete: {req:? }" ) ;
151
152
self . state = ControlState :: CompleteOut ;
152
153
return Some ( req) ;
153
154
}
@@ -159,7 +160,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
159
160
| ControlState :: DataInZlp
160
161
| ControlState :: StatusOut => {
161
162
usb_debug ! (
162
- "Terminating DATA stage early . Current state: {:?}" ,
163
+ "Control transfer completed . Current state: {:?}" ,
163
164
self . state
164
165
) ;
165
166
let _ = self . ep_out . read ( & mut [ ] ) ;
@@ -193,7 +194,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
193
194
return false ;
194
195
}
195
196
196
- usb_trace ! ( "wrote EP0-IN : ZLP" ) ;
197
+ usb_trace ! ( "wrote EP0: ZLP" ) ;
197
198
self . state = ControlState :: DataInLast ;
198
199
}
199
200
ControlState :: DataInLast => {
@@ -205,15 +206,14 @@ impl<B: UsbBus> ControlPipe<'_, B> {
205
206
return true ;
206
207
}
207
208
ControlState :: Idle => {
208
- usb_debug ! ( "Ignoring EP0-IN while in IDLE" ) ;
209
209
// If we received a message on EP0 while sending the last portion of an IN
210
210
// transfer, we may have already transitioned to IDLE without getting the last
211
211
// IN-complete status. Just ignore this indication.
212
212
}
213
213
_ => {
214
- // Unexpected IN packet
215
- usb_debug ! ( "Unexpected EP0-IN. Current state: {:?}" , self . state ) ;
216
- self . set_error ( ) ;
214
+ // If we get IN-COMPLETE indications in unexpected states, it's generally because
215
+ // of control flow in previous phases updating after our packet was successfully
216
+ // sent. Ignore these indications if they don't drive any further behavior.
217
217
}
218
218
} ;
219
219
@@ -229,12 +229,12 @@ impl<B: UsbBus> ControlPipe<'_, B> {
229
229
// There isn't much we can do if the write fails, except to wait for another poll or for
230
230
// the host to resend the request.
231
231
Err ( _err) => {
232
- usb_debug ! ( "Failed to write EP0-IN : {_err:?}" ) ;
232
+ usb_debug ! ( "Failed to write EP0: {_err:?}" ) ;
233
233
return ;
234
234
}
235
235
} ;
236
236
237
- usb_trace ! ( "wrote EP0-IN : {:?}" , & buffer[ self . i..( self . i + count) ] ) ;
237
+ usb_trace ! ( "wrote EP0: {:?}" , & buffer[ self . i..( self . i + count) ] ) ;
238
238
239
239
self . i += count;
240
240
@@ -252,7 +252,10 @@ impl<B: UsbBus> ControlPipe<'_, B> {
252
252
pub fn accept_out ( & mut self ) -> Result < ( ) > {
253
253
match self . state {
254
254
ControlState :: CompleteOut => { }
255
- _ => return Err ( UsbError :: InvalidState ) ,
255
+ _ => {
256
+ usb_debug ! ( "Cannot ACK, invalid state: {:?}" , self . state) ;
257
+ return Err ( UsbError :: InvalidState )
258
+ } ,
256
259
} ;
257
260
258
261
let _ = self . ep_in . write ( & [ ] ) ;
@@ -263,7 +266,10 @@ impl<B: UsbBus> ControlPipe<'_, B> {
263
266
pub fn accept_in ( & mut self , f : impl FnOnce ( & mut [ u8 ] ) -> Result < usize > ) -> Result < ( ) > {
264
267
let req = match self . state {
265
268
ControlState :: CompleteIn ( req) => req,
266
- _ => return Err ( UsbError :: InvalidState ) ,
269
+ _ => {
270
+ usb_debug ! ( "EP0-IN cannot ACK, invalid state: {:?}" , self . state) ;
271
+ return Err ( UsbError :: InvalidState ) ;
272
+ } ,
267
273
} ;
268
274
269
275
let len = f ( & mut self . buf [ ..] ) ?;
@@ -279,7 +285,10 @@ impl<B: UsbBus> ControlPipe<'_, B> {
279
285
pub fn accept_in_static ( & mut self , data : & ' static [ u8 ] ) -> Result < ( ) > {
280
286
let req = match self . state {
281
287
ControlState :: CompleteIn ( req) => req,
282
- _ => return Err ( UsbError :: InvalidState ) ,
288
+ _ => {
289
+ usb_debug ! ( "EP0-IN cannot ACK, invalid state: {:?}" , self . state) ;
290
+ return Err ( UsbError :: InvalidState ) ;
291
+ }
283
292
} ;
284
293
285
294
self . static_in_buf = Some ( data) ;
@@ -297,6 +306,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
297
306
}
298
307
299
308
pub fn reject ( & mut self ) -> Result < ( ) > {
309
+ usb_debug ! ( "EP0 transfer rejected" ) ;
300
310
if !self . waiting_for_response ( ) {
301
311
return Err ( UsbError :: InvalidState ) ;
302
312
}
0 commit comments