usb-gadget 1.2.0

Expose standard or fully custom USB peripherals (gadgets) through a USB device controller (UDC) on Linux.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! Control request tests: recv, recv_all, halt, and drop-without-reply paths.
//!
//! Tests for [`CtrlReceiver`] and [`CtrlSender`], covering the success paths
//! as well as explicit `.halt()` and implicit stall via `Drop`.

use nusb::{
    transfer::{ControlIn, ControlOut, ControlType, Recipient, TransferError},
    MaybeFuture,
};
use std::{
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc,
    },
    thread,
    time::Duration,
};

use usb_gadget::function::custom::{Custom, Event};

use super::*;

const VID: u16 = 0x1234;
const PID: u16 = 0x0030;

/// Vendor request codes for these tests.
mod ctrl_req {
    /// Echo: host sends data, device stores it, then host reads it back.
    pub const ECHO: u8 = 1;
    /// Host sends data, device calls `recv()` (not `recv_all`) with an exact-size buffer.
    pub const RECV_BUF: u8 = 2;
    /// Host sends data, device explicitly calls `halt()` on the CtrlReceiver (rejects it).
    pub const HALT_RECV: u8 = 3;
    /// Host requests data, device explicitly calls `halt()` on the CtrlSender (rejects it).
    pub const HALT_SEND: u8 = 4;
    /// Host sends data, device drops the CtrlReceiver without calling recv or halt.
    pub const DROP_RECV: u8 = 5;
    /// Host requests data, device drops the CtrlSender without calling send or halt.
    pub const DROP_SEND: u8 = 6;
    /// Stop the device event loop.
    pub const STOP: u8 = 255;
}

// ─── Device side ────────────────────────────────────────────────────

fn run_device_ctrl_events(custom: &mut Custom, stop: &AtomicBool) {
    let mut ctrl_data = Vec::new();
    while !stop.load(Ordering::Relaxed) {
        match custom.event_timeout(Duration::from_secs(1)) {
            Ok(Some(event)) => match event {
                Event::SetupHostToDevice(req) => {
                    let code = req.ctrl_req().request;
                    match code {
                        ctrl_req::STOP => {
                            stop.store(true, Ordering::Relaxed);
                            req.recv_all().unwrap();
                        }
                        ctrl_req::ECHO => {
                            ctrl_data = req.recv_all().unwrap();
                        }
                        ctrl_req::RECV_BUF => {
                            // Use recv() with an exact-size buffer instead of recv_all().
                            let len = req.len();
                            let mut buf = vec![0u8; len];
                            let n = req.recv(&mut buf).unwrap();
                            buf.truncate(n);
                            ctrl_data = buf;
                        }
                        ctrl_req::HALT_RECV => {
                            // Explicit halt: reject the host-to-device data.
                            req.halt().unwrap();
                            println!("device: halted CtrlReceiver (request {code})");
                        }
                        ctrl_req::DROP_RECV => {
                            // Drop without calling recv or halt → implicit stall via Drop.
                            println!("device: dropping CtrlReceiver (request {})", code);
                            drop(req);
                        }
                        _ => {
                            // Consume unknown requests to avoid stalling unexpectedly.
                            let _ = req.recv_all();
                        }
                    }
                }
                Event::SetupDeviceToHost(req) => {
                    let code = req.ctrl_req().request;
                    match code {
                        ctrl_req::ECHO => {
                            req.send(&ctrl_data).unwrap();
                        }
                        ctrl_req::HALT_SEND => {
                            // Explicit halt: reject the device-to-host request.
                            req.halt().unwrap();
                            println!("device: halted CtrlSender (request {code})");
                        }
                        ctrl_req::DROP_SEND => {
                            // Drop without calling send or halt → implicit stall via Drop.
                            println!("device: dropping CtrlSender (request {})", code);
                            drop(req);
                        }
                        _ => {
                            let _ = req.send(&[]);
                        }
                    }
                }
                _ => {}
            },
            Ok(None) => {}
            Err(e) => {
                if !stop.load(Ordering::Relaxed) {
                    panic!("device event error: {e}");
                }
                break;
            }
        }
    }
}

// ─── Host side helpers ──────────────────────────────────────────────

fn host_control_out(intf: &nusb::Interface, if_num: u8, request: u8, data: &[u8]) -> Result<(), TransferError> {
    intf.control_out(
        ControlOut {
            control_type: ControlType::Vendor,
            recipient: Recipient::Interface,
            request,
            value: 0,
            index: if_num.into(),
            data,
        },
        Duration::from_secs(2),
    )
    .wait()
}

fn host_control_in(
    intf: &nusb::Interface, if_num: u8, request: u8, length: u16,
) -> Result<Vec<u8>, TransferError> {
    intf.control_in(
        ControlIn {
            control_type: ControlType::Vendor,
            recipient: Recipient::Interface,
            request,
            value: 0,
            index: if_num.into(),
            length,
        },
        Duration::from_secs(2),
    )
    .wait()
}

fn host_stop(intf: &nusb::Interface, if_num: u8) {
    host_control_out(intf, if_num, ctrl_req::STOP, &[]).expect("host: stop control failed");
}

// ─── Tests ──────────────────────────────────────────────────────────

/// Test CtrlReceiver::recv_all (success path) and CtrlSender::send (success path)
/// via an echo round-trip.
#[test]
#[serial]
fn ctrl_recv_all_and_send() {
    init();

    if skip_host() {
        return;
    }

    let (reg, mut custom, _ep_rx, _ep_tx) =
        setup_gadget_with_id(VID, PID, "ctrl recv_all test", "ctrl-recv-all-001");

    let stop = Arc::new(AtomicBool::new(false));
    let stop_dev = stop.clone();

    thread::scope(|s| {
        s.spawn(move || run_device_ctrl_events(&mut custom, &stop_dev));

        s.spawn(|| {
            let (_intf, _ep_in, _ep_out, if_num) = open_host_device(VID, PID);

            let test_data: Vec<u8> = (0..64).collect();

            // Send data to device (CtrlReceiver::recv_all on device side).
            host_control_out(&_intf, if_num, ctrl_req::ECHO, &test_data).expect("host: echo out failed");

            // Read it back (CtrlSender::send on device side).
            let reply = host_control_in(&_intf, if_num, ctrl_req::ECHO, test_data.len() as u16)
                .expect("host: echo in failed");
            assert_eq!(reply.as_slice(), test_data.as_slice(), "echo mismatch");
            println!("host: ctrl_recv_all_and_send echo OK ({} bytes)", test_data.len());

            host_stop(&_intf, if_num);
        });
    });

    thread::sleep(Duration::from_millis(500));
    reg.remove().unwrap();
}

/// Test CtrlReceiver::recv (with explicit buffer) via echo round-trip.
#[test]
#[serial]
fn ctrl_recv_buf() {
    init();

    if skip_host() {
        return;
    }

    let (reg, mut custom, _ep_rx, _ep_tx) =
        setup_gadget_with_id(VID, PID, "ctrl recv_buf test", "ctrl-recv-buf-001");

    let stop = Arc::new(AtomicBool::new(false));
    let stop_dev = stop.clone();

    thread::scope(|s| {
        s.spawn(move || run_device_ctrl_events(&mut custom, &stop_dev));

        s.spawn(|| {
            let (_intf, _ep_in, _ep_out, if_num) = open_host_device(VID, PID);

            let test_data: Vec<u8> = (0..32).collect();

            // Send data to device using RECV_BUF (CtrlReceiver::recv on device side).
            host_control_out(&_intf, if_num, ctrl_req::RECV_BUF, &test_data).expect("host: recv_buf out failed");

            // Read it back via ECHO.
            let reply = host_control_in(&_intf, if_num, ctrl_req::ECHO, test_data.len() as u16)
                .expect("host: echo in failed");
            assert_eq!(reply.as_slice(), test_data.as_slice(), "recv_buf echo mismatch");
            println!("host: ctrl_recv_buf echo OK ({} bytes)", test_data.len());

            host_stop(&_intf, if_num);
        });
    });

    thread::sleep(Duration::from_millis(500));
    reg.remove().unwrap();
}

/// Test CtrlReceiver::halt (explicit stall on host-to-device request).
#[test]
#[serial]
fn ctrl_recv_halt() {
    init();

    if skip_host() {
        return;
    }

    let (reg, mut custom, _ep_rx, _ep_tx) =
        setup_gadget_with_id(VID, PID, "ctrl recv halt test", "ctrl-recv-halt-001");

    let stop = Arc::new(AtomicBool::new(false));
    let stop_dev = stop.clone();

    thread::scope(|s| {
        s.spawn(move || run_device_ctrl_events(&mut custom, &stop_dev));

        s.spawn(|| {
            let (_intf, _ep_in, _ep_out, if_num) = open_host_device(VID, PID);

            let test_data = vec![0xAA; 16];

            // Device will call halt() on the CtrlReceiver → host should see STALL.
            let result = host_control_out(&_intf, if_num, ctrl_req::HALT_RECV, &test_data);
            match result {
                Err(TransferError::Stall) => {
                    println!("host: ctrl_recv_halt got expected STALL");
                }
                Err(e) => panic!("host: ctrl_recv_halt expected Stall, got: {e}"),
                Ok(()) => panic!("host: ctrl_recv_halt expected Stall, but transfer succeeded"),
            }

            // Verify the device still works: a normal echo should succeed.
            let echo_data: Vec<u8> = (0..8).collect();
            host_control_out(&_intf, if_num, ctrl_req::ECHO, &echo_data)
                .expect("host: post-halt echo out failed");
            let reply = host_control_in(&_intf, if_num, ctrl_req::ECHO, echo_data.len() as u16)
                .expect("host: post-halt echo in failed");
            assert_eq!(reply.as_slice(), echo_data.as_slice(), "post-halt echo mismatch");
            println!("host: post-halt echo OK");

            host_stop(&_intf, if_num);
        });
    });

    thread::sleep(Duration::from_millis(500));
    reg.remove().unwrap();
}

/// Test CtrlSender::halt (explicit stall on device-to-host request).
#[test]
#[serial]
fn ctrl_send_halt() {
    init();

    if skip_host() {
        return;
    }

    let (reg, mut custom, _ep_rx, _ep_tx) =
        setup_gadget_with_id(VID, PID, "ctrl send halt test", "ctrl-send-halt-001");

    let stop = Arc::new(AtomicBool::new(false));
    let stop_dev = stop.clone();

    thread::scope(|s| {
        s.spawn(move || run_device_ctrl_events(&mut custom, &stop_dev));

        s.spawn(|| {
            let (_intf, _ep_in, _ep_out, if_num) = open_host_device(VID, PID);

            // Device will call halt() on the CtrlSender → host should see STALL.
            let result = host_control_in(&_intf, if_num, ctrl_req::HALT_SEND, 64);
            match result {
                Err(TransferError::Stall) => {
                    println!("host: ctrl_send_halt got expected STALL");
                }
                Err(e) => panic!("host: ctrl_send_halt expected Stall, got: {e}"),
                Ok(data) => panic!("host: ctrl_send_halt expected Stall, but got {} bytes", data.len()),
            }

            // Verify the device still works after the stall.
            let echo_data: Vec<u8> = (0..8).collect();
            host_control_out(&_intf, if_num, ctrl_req::ECHO, &echo_data)
                .expect("host: post-halt echo out failed");
            let reply = host_control_in(&_intf, if_num, ctrl_req::ECHO, echo_data.len() as u16)
                .expect("host: post-halt echo in failed");
            assert_eq!(reply.as_slice(), echo_data.as_slice(), "post-halt echo mismatch");
            println!("host: post-halt echo OK");

            host_stop(&_intf, if_num);
        });
    });

    thread::sleep(Duration::from_millis(500));
    reg.remove().unwrap();
}

/// Test CtrlReceiver drop without recv or halt (implicit stall via Drop).
#[test]
#[serial]
fn ctrl_recv_drop_stall() {
    init();

    if skip_host() {
        return;
    }

    let (reg, mut custom, _ep_rx, _ep_tx) =
        setup_gadget_with_id(VID, PID, "ctrl recv drop test", "ctrl-recv-drop-001");

    let stop = Arc::new(AtomicBool::new(false));
    let stop_dev = stop.clone();

    thread::scope(|s| {
        s.spawn(move || run_device_ctrl_events(&mut custom, &stop_dev));

        s.spawn(|| {
            let (_intf, _ep_in, _ep_out, if_num) = open_host_device(VID, PID);

            let test_data = vec![0xBB; 16];

            // Device will drop the CtrlReceiver without calling recv or halt → implicit STALL.
            let result = host_control_out(&_intf, if_num, ctrl_req::DROP_RECV, &test_data);
            match result {
                Err(TransferError::Stall) => {
                    println!("host: ctrl_recv_drop_stall got expected STALL");
                }
                Err(e) => panic!("host: ctrl_recv_drop_stall expected Stall, got: {e}"),
                Ok(()) => panic!("host: ctrl_recv_drop_stall expected Stall, but transfer succeeded"),
            }

            // Verify the device still works after the implicit stall.
            let echo_data: Vec<u8> = (0..8).collect();
            host_control_out(&_intf, if_num, ctrl_req::ECHO, &echo_data)
                .expect("host: post-drop echo out failed");
            let reply = host_control_in(&_intf, if_num, ctrl_req::ECHO, echo_data.len() as u16)
                .expect("host: post-drop echo in failed");
            assert_eq!(reply.as_slice(), echo_data.as_slice(), "post-drop echo mismatch");
            println!("host: post-drop echo OK");

            host_stop(&_intf, if_num);
        });
    });

    thread::sleep(Duration::from_millis(500));
    reg.remove().unwrap();
}

/// Test CtrlSender drop without send or halt (implicit stall via Drop).
#[test]
#[serial]
fn ctrl_send_drop_stall() {
    init();

    if skip_host() {
        return;
    }

    let (reg, mut custom, _ep_rx, _ep_tx) =
        setup_gadget_with_id(VID, PID, "ctrl send drop test", "ctrl-send-drop-001");

    let stop = Arc::new(AtomicBool::new(false));
    let stop_dev = stop.clone();

    thread::scope(|s| {
        s.spawn(move || run_device_ctrl_events(&mut custom, &stop_dev));

        s.spawn(|| {
            let (_intf, _ep_in, _ep_out, if_num) = open_host_device(VID, PID);

            // Device will drop the CtrlSender without calling send or halt → implicit STALL.
            let result = host_control_in(&_intf, if_num, ctrl_req::DROP_SEND, 64);
            match result {
                Err(TransferError::Stall) => {
                    println!("host: ctrl_send_drop_stall got expected STALL");
                }
                Err(e) => panic!("host: ctrl_send_drop_stall expected Stall, got: {e}"),
                Ok(data) => panic!("host: ctrl_send_drop_stall expected Stall, but got {} bytes", data.len()),
            }

            // Verify the device still works after the implicit stall.
            let echo_data: Vec<u8> = (0..8).collect();
            host_control_out(&_intf, if_num, ctrl_req::ECHO, &echo_data)
                .expect("host: post-drop echo out failed");
            let reply = host_control_in(&_intf, if_num, ctrl_req::ECHO, echo_data.len() as u16)
                .expect("host: post-drop echo in failed");
            assert_eq!(reply.as_slice(), echo_data.as_slice(), "post-drop echo mismatch");
            println!("host: post-drop echo OK");

            host_stop(&_intf, if_num);
        });
    });

    thread::sleep(Duration::from_millis(500));
    reg.remove().unwrap();
}

/// Stress test: repeated drops in both directions with recovery verification.
///
/// This exercises `clear_prev_event()` across multiple iterations. After each
/// implicit STALL via Drop, we sleep to ensure the device has returned to
/// `event_timeout()` before sending the next request. This forces the next
/// `event()` call to go through `clear_prev_event()` with a potentially stale
/// `setup_event`, catching regressions in EL2HLT handling in both `do_halt()`
/// and `clear_prev_event()`.
#[test]
#[serial]
fn ctrl_drop_recovery_stress() {
    init();

    if skip_host() {
        return;
    }

    let (reg, mut custom, _ep_rx, _ep_tx) =
        setup_gadget_with_id(VID, PID, "ctrl drop stress test", "ctrl-drop-stress-001");

    let stop = Arc::new(AtomicBool::new(false));
    let stop_dev = stop.clone();

    thread::scope(|s| {
        s.spawn(move || run_device_ctrl_events(&mut custom, &stop_dev));

        s.spawn(|| {
            let (_intf, _ep_in, _ep_out, if_num) = open_host_device(VID, PID);

            let rounds = 5;

            for round in 0..rounds {
                println!("host: stress round {}/{rounds}", round + 1);

                // ── Drop a CtrlReceiver (HostToDevice direction) ──
                let result = host_control_out(&_intf, if_num, ctrl_req::DROP_RECV, &[0xAA; 16]);
                match &result {
                    Err(TransferError::Stall) => {}
                    other => panic!("round {round}: DROP_RECV expected Stall, got: {other:?}"),
                }

                // Wait so the device returns to event_timeout() before the next
                // request arrives. This ensures clear_prev_event() runs with the
                // stale setup_event (if do_halt in Drop failed to clear it).
                thread::sleep(Duration::from_millis(50));

                // Verify recovery with two consecutive echo round-trips.
                // Two rounds ensure clear_prev_event runs for both the first
                // post-drop event AND a subsequent normal event.
                for echo_idx in 0..2u8 {
                    let echo_data: Vec<u8> = (0..16).map(|b| b ^ echo_idx).collect();
                    host_control_out(&_intf, if_num, ctrl_req::ECHO, &echo_data).unwrap_or_else(|e| {
                        panic!("round {round}: post-recv-drop echo[{echo_idx}] out failed: {e}")
                    });
                    let reply = host_control_in(&_intf, if_num, ctrl_req::ECHO, echo_data.len() as u16)
                        .unwrap_or_else(|e| {
                            panic!("round {round}: post-recv-drop echo[{echo_idx}] in failed: {e}")
                        });
                    assert_eq!(reply, echo_data, "round {round}: post-recv-drop echo[{echo_idx}] mismatch");
                }

                // ── Drop a CtrlSender (DeviceToHost direction) ──
                let result = host_control_in(&_intf, if_num, ctrl_req::DROP_SEND, 64);
                match &result {
                    Err(TransferError::Stall) => {}
                    other => panic!("round {round}: DROP_SEND expected Stall, got: {other:?}"),
                }

                thread::sleep(Duration::from_millis(50));

                for echo_idx in 0..2u8 {
                    let echo_data: Vec<u8> = (0..16u8).map(|b| b.wrapping_add(echo_idx)).collect();
                    host_control_out(&_intf, if_num, ctrl_req::ECHO, &echo_data).unwrap_or_else(|e| {
                        panic!("round {round}: post-send-drop echo[{echo_idx}] out failed: {e}")
                    });
                    let reply = host_control_in(&_intf, if_num, ctrl_req::ECHO, echo_data.len() as u16)
                        .unwrap_or_else(|e| {
                            panic!("round {round}: post-send-drop echo[{echo_idx}] in failed: {e}")
                        });
                    assert_eq!(reply, echo_data, "round {round}: post-send-drop echo[{echo_idx}] mismatch");
                }
            }

            println!("host: all {rounds} stress rounds passed");
            host_stop(&_intf, if_num);
        });
    });

    thread::sleep(Duration::from_millis(500));
    reg.remove().unwrap();
}