@@ -63,24 +63,26 @@ impl<B: UsbBus> ControlPipe<'_, B> {
63
63
}
64
64
65
65
pub fn reset ( & mut self ) {
66
+ usb_trace ! ( "Control pipe reset" ) ;
66
67
self . state = ControlState :: Idle ;
67
68
}
68
69
69
70
pub fn handle_setup ( & mut self ) -> Option < Request > {
70
71
let count = match self . ep_out . read ( & mut self . buf [ ..] ) {
71
- Ok ( count) => count,
72
+ Ok ( count) => {
73
+ usb_trace ! ( "Read {} bytes on EP0-OUT: {:?}" , count, & self . buf[ ..count] ) ;
74
+ count
75
+ }
72
76
Err ( UsbError :: WouldBlock ) => return None ,
73
77
Err ( _) => {
74
- self . set_error ( ) ;
75
78
return None ;
76
79
}
77
80
} ;
78
81
79
82
let req = match Request :: parse ( & self . buf [ 0 ..count] ) {
80
83
Ok ( req) => req,
81
84
Err ( _) => {
82
- // Failed to parse SETUP packet
83
- self . set_error ( ) ;
85
+ // Failed to parse SETUP packet. We are supposed to silently ignore this.
84
86
return None ;
85
87
}
86
88
} ;
@@ -89,6 +91,8 @@ impl<B: UsbBus> ControlPipe<'_, B> {
89
91
// a stalled state.
90
92
self . ep_out . unstall ( ) ;
91
93
94
+ usb_debug ! ( "EP0 request received: {:?}" , req) ;
95
+
92
96
/*sprintln!("SETUP {:?} {:?} {:?} req:{} val:{} idx:{} len:{} {:?}",
93
97
req.direction, req.request_type, req.recipient,
94
98
req.request, req.value, req.index, req.length,
@@ -102,7 +106,6 @@ impl<B: UsbBus> ControlPipe<'_, B> {
102
106
103
107
if req. length as usize > self . buf . len ( ) {
104
108
// Data stage won't fit in buffer
105
- self . set_error ( ) ;
106
109
return None ;
107
110
}
108
111
@@ -141,9 +144,15 @@ impl<B: UsbBus> ControlPipe<'_, B> {
141
144
}
142
145
} ;
143
146
147
+ usb_trace ! (
148
+ "Read {} bytes on EP0-OUT: {:?}" ,
149
+ count,
150
+ & self . buf[ i..( i + count) ]
151
+ ) ;
144
152
self . i += count;
145
153
146
154
if self . i >= self . len {
155
+ usb_debug ! ( "Request OUT complete: {:?}" , req) ;
147
156
self . state = ControlState :: CompleteOut ;
148
157
return Some ( req) ;
149
158
}
@@ -154,11 +163,19 @@ impl<B: UsbBus> ControlPipe<'_, B> {
154
163
| ControlState :: DataInLast
155
164
| ControlState :: DataInZlp
156
165
| ControlState :: StatusOut => {
166
+ usb_debug ! (
167
+ "Control transfer completed. Current state: {:?}" ,
168
+ self . state
169
+ ) ;
157
170
let _ = self . ep_out . read ( & mut [ ] ) ;
158
171
self . state = ControlState :: Idle ;
159
172
}
160
173
_ => {
161
174
// Discard the packet
175
+ usb_debug ! (
176
+ "Discarding EP0 data due to unexpected state. Current state: {:?}" ,
177
+ self . state
178
+ ) ;
162
179
let _ = self . ep_out . read ( & mut [ ] ) ;
163
180
164
181
// Unexpected OUT packet
@@ -181,6 +198,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
181
198
return false ;
182
199
}
183
200
201
+ usb_trace ! ( "wrote EP0: ZLP" ) ;
184
202
self . state = ControlState :: DataInLast ;
185
203
}
186
204
ControlState :: DataInLast => {
@@ -197,8 +215,9 @@ impl<B: UsbBus> ControlPipe<'_, B> {
197
215
// IN-complete status. Just ignore this indication.
198
216
}
199
217
_ => {
200
- // Unexpected IN packet
201
- self . set_error ( ) ;
218
+ // If we get IN-COMPLETE indications in unexpected states, it's generally because
219
+ // of control flow in previous phases updating after our packet was successfully
220
+ // sent. Ignore these indications if they don't drive any further behavior.
202
221
}
203
222
} ;
204
223
@@ -213,9 +232,14 @@ impl<B: UsbBus> ControlPipe<'_, B> {
213
232
Ok ( c) => c,
214
233
// There isn't much we can do if the write fails, except to wait for another poll or for
215
234
// the host to resend the request.
216
- Err ( _) => return ,
235
+ Err ( _err) => {
236
+ usb_debug ! ( "Failed to write EP0: {:?}" , _err) ;
237
+ return ;
238
+ }
217
239
} ;
218
240
241
+ usb_trace ! ( "wrote EP0: {:?}" , & buffer[ self . i..( self . i + count) ] ) ;
242
+
219
243
self . i += count;
220
244
221
245
if self . i >= self . len {
@@ -232,7 +256,10 @@ impl<B: UsbBus> ControlPipe<'_, B> {
232
256
pub fn accept_out ( & mut self ) -> Result < ( ) > {
233
257
match self . state {
234
258
ControlState :: CompleteOut => { }
235
- _ => return Err ( UsbError :: InvalidState ) ,
259
+ _ => {
260
+ usb_debug ! ( "Cannot ACK, invalid state: {:?}" , self . state) ;
261
+ return Err ( UsbError :: InvalidState ) ;
262
+ }
236
263
} ;
237
264
238
265
let _ = self . ep_in . write ( & [ ] ) ;
@@ -243,7 +270,10 @@ impl<B: UsbBus> ControlPipe<'_, B> {
243
270
pub fn accept_in ( & mut self , f : impl FnOnce ( & mut [ u8 ] ) -> Result < usize > ) -> Result < ( ) > {
244
271
let req = match self . state {
245
272
ControlState :: CompleteIn ( req) => req,
246
- _ => return Err ( UsbError :: InvalidState ) ,
273
+ _ => {
274
+ usb_debug ! ( "EP0-IN cannot ACK, invalid state: {:?}" , self . state) ;
275
+ return Err ( UsbError :: InvalidState ) ;
276
+ }
247
277
} ;
248
278
249
279
let len = f ( & mut self . buf [ ..] ) ?;
@@ -259,7 +289,10 @@ impl<B: UsbBus> ControlPipe<'_, B> {
259
289
pub fn accept_in_static ( & mut self , data : & ' static [ u8 ] ) -> Result < ( ) > {
260
290
let req = match self . state {
261
291
ControlState :: CompleteIn ( req) => req,
262
- _ => return Err ( UsbError :: InvalidState ) ,
292
+ _ => {
293
+ usb_debug ! ( "EP0-IN cannot ACK, invalid state: {:?}" , self . state) ;
294
+ return Err ( UsbError :: InvalidState ) ;
295
+ }
263
296
} ;
264
297
265
298
self . static_in_buf = Some ( data) ;
@@ -277,6 +310,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
277
310
}
278
311
279
312
pub fn reject ( & mut self ) -> Result < ( ) > {
313
+ usb_debug ! ( "EP0 transfer rejected" ) ;
280
314
if !self . waiting_for_response ( ) {
281
315
return Err ( UsbError :: InvalidState ) ;
282
316
}
@@ -286,6 +320,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
286
320
}
287
321
288
322
fn set_error ( & mut self ) {
323
+ usb_debug ! ( "EP0 stalled - error" ) ;
289
324
self . state = ControlState :: Error ;
290
325
self . ep_out . stall ( ) ;
291
326
self . ep_in . stall ( ) ;
0 commit comments