probe_rs/probe/jlink/
mod.rs

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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
//! Support for J-Link Debug probes

mod bits;
pub mod capabilities;
mod config;
mod connection;
mod error;
mod interface;
mod speed;
pub mod swo;

use std::fmt;
use std::iter;
use std::time::{Duration, Instant};

use bitvec::prelude::*;

use itertools::Itertools;
use nusb::transfer::{Direction, EndpointType};
use nusb::DeviceInfo;
use probe_rs_target::ScanChainElement;

use self::bits::BitIter;
use self::capabilities::{Capabilities, Capability};
use self::error::JlinkError;
use self::interface::{Interface, Interfaces};
use self::speed::SpeedConfig;
use self::swo::SwoMode;
use crate::architecture::arm::{ArmError, Pins};
use crate::architecture::xtensa::communication_interface::{
    XtensaCommunicationInterface, XtensaDebugInterfaceState,
};
use crate::probe::common::{JtagDriverState, RawJtagIo};
use crate::probe::jlink::bits::IteratorExt;
use crate::probe::jlink::config::JlinkConfig;
use crate::probe::jlink::connection::JlinkConnection;
use crate::probe::usb_util::InterfaceExt;
use crate::probe::JTAGAccess;
use crate::{
    architecture::{
        arm::{
            communication_interface::{DapProbe, UninitializedArmProbe},
            swo::SwoConfig,
            ArmCommunicationInterface, SwoAccess,
        },
        riscv::{communication_interface::RiscvInterfaceBuilder, dtm::jtag_dtm::JtagDtmBuilder},
    },
    probe::{
        arm_debug_interface::{ProbeStatistics, RawProtocolIo, SwdSettings},
        DebugProbe, DebugProbeError, DebugProbeInfo, DebugProbeSelector, ProbeFactory,
        WireProtocol,
    },
};

const SWO_BUFFER_SIZE: u16 = 128;
const TIMEOUT_DEFAULT: Duration = Duration::from_millis(500);

/// Factory to create [`JLink`] probes.
#[derive(Debug)]
pub struct JLinkFactory;

impl std::fmt::Display for JLinkFactory {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("J-Link")
    }
}

impl ProbeFactory for JLinkFactory {
    fn open(&self, selector: &DebugProbeSelector) -> Result<Box<dyn DebugProbe>, DebugProbeError> {
        fn open_error(e: std::io::Error, while_: &'static str) -> DebugProbeError {
            let help = if cfg!(windows) {
                "(this error may be caused by not having the WinUSB driver installed; use Zadig (https://zadig.akeo.ie/) to install it for the J-Link device; this will replace the SEGGER J-Link driver)"
            } else {
                ""
            };

            DebugProbeError::Usb(std::io::Error::other(format!(
                "error while {while_}: {e}{help}",
            )))
        }

        let mut jlinks = nusb::list_devices()
            .map_err(DebugProbeError::Usb)?
            .filter(is_jlink)
            .filter(|info| selector.matches(info))
            .collect::<Vec<_>>();

        if jlinks.is_empty() {
            return Err(DebugProbeError::ProbeCouldNotBeCreated(
                super::ProbeCreationError::NotFound,
            ));
        } else if jlinks.len() > 1 {
            tracing::warn!("More than one matching J-Link was found. Opening the first one.")
        }

        let info = jlinks.pop().unwrap();

        let handle = info
            .open()
            .map_err(|e| open_error(e, "opening the USB device"))?;

        let configs: Vec<_> = handle.configurations().collect();

        if configs.len() != 1 {
            tracing::warn!("device has {} configurations, expected 1", configs.len());
        }

        let conf = &configs[0];
        tracing::debug!("scanning {} interfaces", conf.interfaces().count());
        tracing::trace!("active configuration descriptor: {:#x?}", conf);

        let mut jlink_intf = None;
        for intf in conf.interfaces() {
            tracing::trace!("interface #{} descriptors:", intf.interface_number());

            for descr in intf.alt_settings() {
                tracing::trace!("{:#x?}", descr);

                // We detect the proprietary J-Link interface using the vendor-specific class codes
                // and the endpoint properties
                if descr.class() == 0xff && descr.subclass() == 0xff && descr.protocol() == 0xff {
                    if let Some((intf, _, _, _)) = jlink_intf {
                        Err(JlinkError::Other(format!(
                            "found multiple matching USB interfaces ({} and {})",
                            intf,
                            descr.interface_number()
                        )))?;
                    }

                    let endpoints: Vec<_> = descr.endpoints().collect();
                    tracing::trace!("endpoint descriptors: {:#x?}", endpoints);
                    if endpoints.len() != 2 {
                        tracing::warn!("vendor-specific interface with {} endpoints, expected 2 (skipping interface)", endpoints.len());
                        continue;
                    }

                    if !endpoints
                        .iter()
                        .all(|ep| ep.transfer_type() == EndpointType::Bulk)
                    {
                        tracing::warn!(
                            "encountered non-bulk endpoints, skipping interface: {:#x?}",
                            endpoints
                        );
                        continue;
                    }

                    let (read_ep, write_ep) = if endpoints[0].direction() == Direction::In {
                        (&endpoints[0], &endpoints[1])
                    } else {
                        (&endpoints[1], &endpoints[0])
                    };

                    jlink_intf = Some((
                        descr.interface_number(),
                        read_ep.address(),
                        write_ep.address(),
                        read_ep.max_packet_size(),
                    ));
                    tracing::debug!("J-Link interface is #{}", descr.interface_number());
                }
            }
        }

        let Some((intf, read_ep, write_ep, max_read_ep_packet)) = jlink_intf else {
            Err(JlinkError::Other(
                "device is not a J-Link device".to_string(),
            ))?
        };

        let handle = handle
            .claim_interface(intf)
            .map_err(|e| open_error(e, "taking control over USB device"))?;

        let mut this = JLink {
            read_ep,
            write_ep,
            max_read_ep_packet,
            caps: Capabilities::from_raw_legacy(0), // dummy value
            interface: Interface::Spi,              // dummy value, must not be JTAG
            interfaces: Interfaces::from_bits_warn(0), // dummy value
            handle,

            supported_protocols: vec![],  // dummy value
            protocol: WireProtocol::Jtag, // dummy value
            connection_handle: None,

            swo_config: None,
            speed_khz: 0, // default is unknown
            swd_settings: SwdSettings::default(),
            probe_statistics: ProbeStatistics::default(),
            jtag_state: JtagDriverState::default(),

            jtag_tms_bits: vec![],
            jtag_tdi_bits: vec![],
            jtag_capture_tdo: vec![],
            jtag_response: BitVec::new(),

            max_mem_block_size: 0, // dummy value
            jtag_chunk_size: 0,    // dummy value

            config: JlinkConfig::default(),
        };
        this.fill_capabilities()?;
        this.fill_interfaces()?;

        this.supported_protocols = if this.caps.contains(Capability::SelectIf) {
            this.interfaces
                .into_iter()
                .filter_map(|p| match WireProtocol::try_from(p) {
                    Ok(protocol) => Some(protocol),
                    Err(JlinkError::UnknownInterface(interface)) => {
                        // We ignore unknown protocols.
                        tracing::debug!(
                            "J-Link returned interface {:?}, which is not supported by probe-rs.",
                            interface
                        );
                        None
                    }
                    Err(_) => None,
                })
                .collect::<Vec<_>>()
        } else {
            // The J-Link cannot report which interfaces it supports, and cannot
            // switch interfaces. We assume it just supports JTAG.
            vec![WireProtocol::Jtag]
        };

        this.protocol = if this.supported_protocols.contains(&WireProtocol::Swd) {
            // Default to SWD if supported, since it's the most commonly used.
            WireProtocol::Swd
        } else {
            // Otherwise just pick the first supported.
            *this.supported_protocols.first().unwrap()
        };

        if this.caps.contains(Capability::GetMaxBlockSize) {
            this.max_mem_block_size = this.read_max_mem_block()?;

            tracing::debug!(
                "J-Link max mem block size for SWD IO: {} byte",
                this.max_mem_block_size
            );
        } else {
            tracing::debug!(
                "J-Link does not support GET_MAX_MEM_BLOCK, using default value of 65535"
            );
            this.max_mem_block_size = 65535;
        }

        // Some devices can't handle large transfers, so we limit the chunk size.
        // While it would be nice to read this directly from the device,
        // `read_max_mem_block`'s return value does not directly correspond to the
        // maximum transfer size when performing JTAG IO, and it's not clear how to get the actual value.
        // The number of *bits* is encoded as a u16, so the maximum value is 65535
        this.jtag_chunk_size = match selector.product_id {
            // 0x0101: J-Link EDU
            0x0101 => 65535,
            // 0x1051: J-Link OB-K22-SiFive: 504 bits
            0x1051 => 504,
            // Assume the lowest value is a safe default
            _ => 504,
        };
        this.config = this.read_device_config()?;
        this.connection_handle = if requires_connection_handle(selector) {
            Some(this.register_connection()?)
        } else {
            None
        };

        Ok(Box::new(this))
    }

    fn list_probes(&self) -> Vec<DebugProbeInfo> {
        list_jlink_devices()
    }
}

fn requires_connection_handle(selector: &DebugProbeSelector) -> bool {
    // These devices require a connection handle to be registered before they can be used.
    // As some other devices can't handle the registration command, we only enable it for known
    // devices.
    let devices = [
        (0x1366, 0x0101, Some("000000123456")), // Blue J-Link PRO clone
    ];

    devices.contains(&(
        selector.vendor_id,
        selector.product_id,
        selector.serial_number.as_deref(),
    ))
}

impl Drop for JLink {
    fn drop(&mut self) {
        self.unregister_connection().ok();
    }
}

#[repr(u8)]
#[allow(dead_code)]
enum Command {
    Version = 0x01,
    Register = 0x09,
    GetSpeeds = 0xC0,
    GetMaxMemBlock = 0xD4,
    GetCaps = 0xE8,
    GetCapsEx = 0xED,
    GetHwVersion = 0xF0,

    GetState = 0x07,
    GetHwInfo = 0xC1,
    GetCounters = 0xC2,
    MeasureRtckReact = 0xF6,

    ResetTrst = 0x02,
    SetSpeed = 0x05,
    SelectIf = 0xC7,
    SetKsPower = 0x08,
    HwClock = 0xC8,
    HwTms0 = 0xC9,
    HwTms1 = 0xCA,
    HwData0 = 0xCB,
    HwData1 = 0xCC,
    HwJtag = 0xCD,
    HwJtag2 = 0xCE,
    HwJtag3 = 0xCF,
    HwJtagWrite = 0xD5,
    HwJtagGetResult = 0xD6,
    HwTrst0 = 0xDE,
    HwTrst1 = 0xDF,
    Swo = 0xEB,
    WriteDcc = 0xF1,

    ResetTarget = 0x03,
    HwReleaseResetStopEx = 0xD0,
    HwReleaseResetStopTimed = 0xD1,
    HwReset0 = 0xDC,
    HwReset1 = 0xDD,
    GetCpuCaps = 0xE9,
    ExecCpuCmd = 0xEA,
    WriteMem = 0xF4,
    ReadMem = 0xF5,
    WriteMemArm79 = 0xF7,
    ReadMemArm79 = 0xF8,

    ReadConfig = 0xF2,
    WriteConfig = 0xF3,
}

/// A J-Link probe.
pub struct JLink {
    handle: nusb::Interface,

    read_ep: u8,
    write_ep: u8,
    max_read_ep_packet: usize,

    /// The capabilities reported by the device. They're fetched once, when the device is opened.
    caps: Capabilities,

    /// The supported interfaces. Like `caps`, this is fetched once when opening the device.
    interfaces: Interfaces,

    /// The currently selected target interface. This is stored here to avoid unnecessary roundtrips
    /// when performing target I/O operations.
    interface: Interface,

    /// Device configuration, fetched once when the device is opened.
    config: JlinkConfig,
    connection_handle: Option<u16>,

    swo_config: Option<SwoConfig>,

    /// Protocols supported by the probe.
    supported_protocols: Vec<WireProtocol>,
    /// Protocol chosen by the user
    protocol: WireProtocol,

    speed_khz: u32,

    jtag_tms_bits: Vec<bool>,
    jtag_tdi_bits: Vec<bool>,
    jtag_capture_tdo: Vec<bool>,
    jtag_response: BitVec<u8, Lsb0>,
    jtag_state: JtagDriverState,

    /// max number of bits in a transfer chunk, when using JTAG
    jtag_chunk_size: usize,

    /// Maximum memory block size, as report by the `GET_MAX_MEM_BLOCK` command.
    ///
    /// Used to determine maximum transfer length for SWD IO.
    max_mem_block_size: u32,

    probe_statistics: ProbeStatistics,
    swd_settings: SwdSettings,
}

impl fmt::Debug for JLink {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("JLink").finish()
    }
}

impl JLink {
    /// Returns the supported J-Link capabilities.
    pub fn capabilities(&self) -> Capabilities {
        self.caps
    }

    /// Reads the advertised capabilities from the device.
    fn fill_capabilities(&mut self) -> Result<(), JlinkError> {
        self.write_cmd(&[Command::GetCaps as u8])?;

        let caps = self.read_u32().map(Capabilities::from_raw_legacy)?;

        tracing::debug!("legacy caps: {:?}", caps);

        // If the `GET_CAPS_EX` capability is set, use the extended capability command to fetch
        // all the capabilities.
        if caps.contains(Capability::GetCapsEx) {
            self.write_cmd(&[Command::GetCapsEx as u8])?;

            let real_caps = self.read_n::<32>().map(Capabilities::from_raw_ex)?;
            if !real_caps.contains_all(caps) {
                return Err(JlinkError::Other(format!(
                    "ext. caps are not a superset of legacy caps (legacy: {:?}, ex: {:?})",
                    caps, real_caps
                )));
            }
            tracing::debug!("extended caps: {:?}", real_caps);
            self.caps = real_caps;
        } else {
            tracing::debug!("extended caps not supported");
            self.caps = caps;
        }

        Ok(())
    }

    fn fill_interfaces(&mut self) -> Result<(), JlinkError> {
        if !self.caps.contains(Capability::SelectIf) {
            // Pre-SELECT_IF probes only support JTAG.
            self.interfaces = Interfaces::single(Interface::Jtag);
            self.interface = Interface::Jtag;

            return Ok(());
        }

        self.write_cmd(&[Command::SelectIf as u8, 0xFF])?;

        self.interfaces = self.read_u32().map(Interfaces::from_bits_warn)?;

        Ok(())
    }

    fn write_cmd(&self, cmd: &[u8]) -> Result<(), JlinkError> {
        tracing::trace!("write {} bytes: {:x?}", cmd.len(), cmd);

        let n = self
            .handle
            .write_bulk(self.write_ep, cmd, TIMEOUT_DEFAULT)?;

        if n != cmd.len() {
            return Err(JlinkError::Other(format!(
                "incomplete write (expected {} bytes, wrote {})",
                cmd.len(),
                n
            )));
        }
        Ok(())
    }

    fn read(&self, buf: &mut [u8]) -> Result<(), JlinkError> {
        let needs_workaround = buf.len() % self.max_read_ep_packet == 0;
        let len = buf.len();

        let mut tmp_buffer;
        let dst = if needs_workaround {
            // For some unknown reason, reading 256 bytes of config data leaves the interface in
            // an unusable state. Force-reading one more byte works around this issue.
            tmp_buffer = vec![0; len + 1];
            &mut tmp_buffer
        } else {
            tmp_buffer = vec![];
            &mut buf[..]
        };

        let mut total = 0;
        while total < len {
            let n = self
                .handle
                .read_bulk(self.read_ep, &mut dst[total..], TIMEOUT_DEFAULT)?;

            total += n;
        }

        if needs_workaround {
            buf.copy_from_slice(&tmp_buffer[..len]);
        }

        tracing::trace!("read {total} bytes: {buf:x?}");

        Ok(())
    }

    fn read_n<const N: usize>(&self) -> Result<[u8; N], JlinkError> {
        let mut buf = [0; N];
        self.read(&mut buf)?;
        Ok(buf)
    }

    fn read_u16(&self) -> Result<u16, JlinkError> {
        self.read_n::<2>().map(u16::from_le_bytes)
    }

    fn read_u32(&self) -> Result<u32, JlinkError> {
        self.read_n::<4>().map(u32::from_le_bytes)
    }

    fn require_capability(&self, cap: Capability) -> Result<(), JlinkError> {
        if self.caps.contains(cap) {
            Ok(())
        } else {
            Err(JlinkError::MissingCapability(cap))
        }
    }

    fn require_interface_supported(&self, intf: Interface) -> Result<(), JlinkError> {
        if self.interfaces.contains(intf) {
            Ok(())
        } else {
            Err(JlinkError::InterfaceNotSupported(intf))
        }
    }

    fn require_interface_selected(&self, intf: Interface) -> Result<(), JlinkError> {
        if self.interface == intf {
            Ok(())
        } else {
            Err(JlinkError::WrongInterfaceSelected {
                selected: self.interface,
                needed: intf,
            })
        }
    }

    /// Reads the maximum mem block size in Bytes.
    ///
    /// This requires the probe to support [`Capability::GetMaxBlockSize`].
    pub fn read_max_mem_block(&self) -> Result<u32, JlinkError> {
        // This cap refers to a nonexistent command `GET_MAX_BLOCK_SIZE`, but it probably means
        // `GET_MAX_MEM_BLOCK`.
        self.require_capability(Capability::GetMaxBlockSize)?;

        self.write_cmd(&[Command::GetMaxMemBlock as u8])?;

        self.read_u32()
    }

    fn read_device_config(&self) -> Result<JlinkConfig, JlinkError> {
        if self.caps.contains(Capability::ReadConfig) {
            self.write_cmd(&[Command::ReadConfig as u8])?;
            let bytes = self.read_n::<256>()?;

            let config = match JlinkConfig::parse(bytes) {
                Ok(config) => {
                    tracing::debug!("J-Link config: {:?}", config);
                    config
                }
                Err(error) => {
                    tracing::warn!("Failed to parse J-Link config: {error}");
                    JlinkConfig::default()
                }
            };

            Ok(config)
        } else {
            Ok(JlinkConfig::default())
        }
    }

    /// Reads the firmware version string from the device.
    fn read_firmware_version(&self) -> Result<String, JlinkError> {
        self.write_cmd(&[Command::Version as u8])?;

        let num_bytes = self.read_u16()?;
        let mut buf = vec![0; num_bytes as usize];
        self.read(&mut buf)?;

        Ok(String::from_utf8_lossy(
            // The firmware version string returned may contain null bytes. If
            // this happens, only return the preceding bytes.
            match buf.iter().position(|&b| b == 0) {
                Some(pos) => &buf[..pos],
                None => &buf,
            },
        )
        .into_owned())
    }

    /// Reads the hardware version from the device.
    ///
    /// This requires the probe to support [`Capability::GetHwVersion`].
    fn read_hardware_version(&self) -> Result<HardwareVersion, JlinkError> {
        self.require_capability(Capability::GetHwVersion)?;

        self.write_cmd(&[Command::GetHwVersion as u8])?;

        self.read_u32().map(HardwareVersion::from_u32)
    }

    /// Selects the interface to use for talking to the target MCU.
    ///
    /// Switching interfaces will reset the configured transfer speed, so [`JLink::set_speed`]
    /// needs to be called *after* `select_interface`.
    ///
    /// This requires the probe to support [`Capability::SelectIf`].
    ///
    /// **Note**: Selecting a different interface may cause the J-Link to perform target I/O!
    fn select_interface(&mut self, intf: Interface) -> Result<(), JlinkError> {
        if self.interface == intf {
            return Ok(());
        }

        self.require_capability(Capability::SelectIf)?;

        self.require_interface_supported(intf)?;

        self.write_cmd(&[Command::SelectIf as u8, intf as u8])?;

        // Returns the previous interface, ignore it
        let _ = self.read_u32()?;

        self.interface = intf;

        if self.speed_khz != 0 {
            // SelectIf resets the configured speed. Let's restore it.
            self.set_interface_clock_speed(SpeedConfig::khz(self.speed_khz as u16).unwrap())?;
        }

        Ok(())
    }

    /// Changes the state of the RESET pin (pin 15).
    ///
    /// RESET is an open-collector / open-drain output. If `reset` is `true`, the output will float.
    /// If `reset` is `false`, the output will be pulled to ground.
    ///
    /// **Note**: Some embedded J-Link probes may not expose this pin or may not allow controlling
    /// it using this function.
    fn set_reset(&mut self, reset: bool) -> Result<(), JlinkError> {
        let cmd = if reset {
            Command::HwReset1
        } else {
            Command::HwReset0
        };
        self.write_cmd(&[cmd as u8])
    }

    /// Resets the target's JTAG TAP controller by temporarily asserting (n)TRST (Pin 3).
    ///
    /// This might not do anything if the pin is not connected to the target. It does not affect
    /// non-JTAG target interfaces.
    fn reset_trst(&mut self) -> Result<(), JlinkError> {
        self.write_cmd(&[Command::ResetTrst as u8])
    }

    /// Reads the target voltage measured on the `VTref` pin, in millivolts.
    ///
    /// In order to use the J-Link, this voltage must be present, since it will be used as the level
    /// of the I/O signals to the target.
    fn read_target_voltage(&self) -> Result<u16, JlinkError> {
        self.write_cmd(&[Command::GetState as u8])?;

        let buf = self.read_n::<8>()?;

        let voltage = [buf[0], buf[1]];
        Ok(u16::from_le_bytes(voltage))
    }

    fn shift_jtag_bit(
        &mut self,
        tms: bool,
        tdi: bool,
        capture: bool,
    ) -> Result<(), DebugProbeError> {
        self.jtag_state.state.update(tms);

        self.jtag_tms_bits.push(tms);
        self.jtag_tdi_bits.push(tdi);
        self.jtag_capture_tdo.push(capture);

        if self.jtag_tms_bits.len() >= self.jtag_chunk_size {
            self.flush_jtag()?;
        }

        Ok(())
    }

    fn flush_jtag(&mut self) -> Result<(), JlinkError> {
        if self.jtag_tms_bits.is_empty() {
            return Ok(());
        }

        self.require_interface_selected(Interface::Jtag)?;

        let mut has_status_byte = false;
        // There's 3 commands for doing a JTAG transfer. The older 2 are obsolete with hardware
        // version 5 and above, which adds the 3rd command. Unfortunately we cannot reliably use the
        // HW version to determine this since some embedded J-Link probes have a HW version of
        // 1.0.0, but still support SWD, so we use the `SELECT_IF` capability instead.
        let cmd = if self.caps.contains(Capability::SelectIf) {
            // Use the new JTAG3 command, make sure to select the JTAG interface mode
            has_status_byte = true;
            Command::HwJtag3
        } else {
            // Use the legacy JTAG2 command
            // FIXME is HW_JTAG relevant at all?
            Command::HwJtag2
        };

        let tms_bit_count = self.jtag_tms_bits.len();
        let tdi_bit_count = self.jtag_tdi_bits.len();
        assert_eq!(
            tms_bit_count, tdi_bit_count,
            "TMS and TDI must have the same number of bits"
        );
        let capacity = 1 + 1 + 2 + tms_bit_count.div_ceil(8) * 2;
        let mut buf = Vec::with_capacity(capacity);
        buf.resize(4, 0);
        buf[0] = cmd as u8;
        // JTAG3 and JTAG2 use the same format for JTAG operations
        // buf[1] is dummy data for alignment
        // buf[2..=3] is the bit count
        let bit_count = u16::try_from(tms_bit_count).expect("too much data to transfer");
        buf[2..=3].copy_from_slice(&bit_count.to_le_bytes());
        buf.extend(self.jtag_tms_bits.drain(..).collapse_bytes());
        buf.extend(self.jtag_tdi_bits.drain(..).collapse_bytes());

        self.write_cmd(&buf)?;

        // Round bit count up to multple of 8 to get the number of response bytes.
        let num_resp_bytes = tms_bit_count.div_ceil(8);
        tracing::trace!(
            "{} TMS/TDI bits sent; reading {} response bytes",
            tms_bit_count,
            num_resp_bytes
        );

        // Response is `num_resp_bytes` TDO data bytes and one status byte,
        // if the JTAG3 command is used.
        let mut read_len = num_resp_bytes;

        if has_status_byte {
            read_len += 1;
        }

        self.read(&mut buf[..read_len])?;

        // Check the status if a JTAG3 command was used.
        if has_status_byte && buf[read_len - 1] != 0 {
            return Err(JlinkError::Other(format!(
                "probe I/O command returned error code {:#x}",
                buf[read_len - 1]
            )));
        }

        let response = BitIter::new(&buf[..num_resp_bytes], tms_bit_count);

        for (bit, capture) in response.zip(self.jtag_capture_tdo.drain(..)) {
            if capture {
                self.jtag_response.push(bit);
            }
        }

        Ok(())
    }

    fn read_captured_bits(&mut self) -> Result<BitVec<u8, Lsb0>, DebugProbeError> {
        self.flush_jtag()?;

        Ok(std::mem::take(&mut self.jtag_response))
    }

    /// Perform a single SWDIO command
    ///
    /// The caller needs to ensure that the given iterators are not longer than the maximum transfer size
    /// allowed. It seems that the maximum transfer size is determined by [`self.max_mem_block_size`].
    fn perform_swdio_transfer<D, S>(&self, dir: D, swdio: S) -> Result<Vec<bool>, DebugProbeError>
    where
        D: IntoIterator<Item = bool>,
        S: IntoIterator<Item = bool>,
    {
        self.require_interface_selected(Interface::Swd)?;

        const COMMAND_OVERHEAD: u32 = 4;

        let max_bits = ((self.max_mem_block_size - COMMAND_OVERHEAD) / 2 * 8) as usize;
        let max_bits = max_bits.min(65535);

        let dir_chunks = dir.into_iter().chunks(max_bits);
        let swdio_chunks = swdio.into_iter().chunks(max_bits);

        #[allow(clippy::useless_conversion)]
        let chunks = dir_chunks.into_iter().zip(swdio_chunks.into_iter());

        let mut output = Vec::new();
        let mut buf = Vec::with_capacity(self.max_mem_block_size as usize);
        buf.resize(4, 0);
        buf[0] = Command::HwJtag3 as u8;
        // buf[1] is dummy data for alignment
        buf[1] = 0;
        // buf[2..=3] is the bit count, which we'll fill in later

        for (dir, swdio) in chunks {
            buf.truncate(4);

            let mut dir_bit_count = 0;
            buf.extend(dir.inspect(|_| dir_bit_count += 1).collapse_bytes());
            let mut swdio_bit_count = 0;
            buf.extend(swdio.inspect(|_| swdio_bit_count += 1).collapse_bytes());

            assert_eq!(
                dir_bit_count, swdio_bit_count,
                "`dir` and `swdio` must have the same number of bits"
            );

            let num_bits = dir_bit_count as u16;
            buf[2..=3].copy_from_slice(&num_bits.to_le_bytes());
            let num_bytes = usize::from(num_bits.div_ceil(8));

            tracing::trace!("Buffer length for j-link transfer: {}", buf.len());

            self.write_cmd(&buf)?;

            // Response is `num_bytes` SWDIO data bytes and one status byte
            self.read(&mut buf[..num_bytes + 1])?;

            if buf[num_bytes] != 0 {
                return Err(JlinkError::Other(format!(
                    "probe I/O command returned error code {:#x}",
                    buf[num_bytes]
                ))
                .into());
            }

            output.extend(BitIter::new(&buf[..num_bytes], num_bits as usize));
        }

        Ok(output)
    }

    /// Enable/Disable the Target Power Supply of the probe.
    ///
    /// This is not available on all J-Links.
    pub fn set_kickstart_power(&mut self, enable: bool) -> Result<(), JlinkError> {
        self.require_capability(Capability::SetKsPower)?;
        self.write_cmd(&[Command::SetKsPower as u8, if enable { 1 } else { 0 }])
    }

    fn register_connection(&mut self) -> Result<u16, JlinkError> {
        if !self.caps.contains(Capability::Register) {
            return Ok(0);
        }

        // Undocumented, taken from OpenOCD/libjaylink
        let mut buf = vec![Command::Register as u8, 0x64];
        buf.extend(JlinkConnection::usb(0).into_bytes());
        self.write_cmd(&buf)?;

        let handle = self.read_registration_response()?;

        if handle == 0 {
            return Err(JlinkError::Other("Invalid registration handle".to_string()));
        }

        Ok(handle)
    }

    fn unregister_connection(&mut self) -> Result<(), JlinkError> {
        if !self.caps.contains(Capability::Register) {
            return Ok(());
        }

        if let Some(handle) = self.connection_handle.take() {
            let mut buf = vec![Command::Register as u8, 0x65];
            buf.extend(JlinkConnection::usb(handle).into_bytes());
            self.write_cmd(&buf)?;
            self.read_registration_response()?;
        }

        Ok(())
    }

    fn read_registration_response(&mut self) -> Result<u16, JlinkError> {
        const REG_HEADER_SIZE: usize = 8;
        const REG_MIN_SIZE: usize = 76;
        const REG_MAX_SIZE: usize = 512;

        let mut response = [0; REG_MAX_SIZE];
        self.read(&mut response[..REG_MIN_SIZE])?;

        let handle = u16::from_le_bytes([response[0], response[1]]);
        let num = u16::from_le_bytes([response[2], response[3]]) as usize;
        let entry_size = u16::from_le_bytes([response[4], response[5]]) as usize;
        let info_size = u16::from_le_bytes([response[6], response[7]]) as usize;

        let table_size = num * entry_size;
        let size = REG_HEADER_SIZE + table_size + info_size;

        tracing::debug!("Registration response size: {size}");

        if size > REG_MAX_SIZE {
            return Err(JlinkError::Other(format!(
                "Maximum registration size exceeded: {size} bytes",
            )));
        }

        if size > REG_MIN_SIZE {
            // Read the rest of the response.
            self.read(&mut response[REG_MIN_SIZE..size])?;
        }

        // TODO: we should process the response, and return the list of connections.

        Ok(handle)
    }
}

impl DebugProbe for JLink {
    fn select_protocol(&mut self, protocol: WireProtocol) -> Result<(), DebugProbeError> {
        if self.caps.contains(Capability::SelectIf) {
            let jlink_interface = match protocol {
                WireProtocol::Swd => Interface::Swd,
                WireProtocol::Jtag => Interface::Jtag,
            };

            if !self.interfaces.contains(jlink_interface) {
                return Err(DebugProbeError::UnsupportedProtocol(protocol));
            }
        } else {
            // Assume JTAG protocol if the probe does not support switching interfaces
            if protocol != WireProtocol::Jtag {
                return Err(DebugProbeError::UnsupportedProtocol(protocol));
            }
        }

        self.protocol = protocol;

        Ok(())
    }

    fn active_protocol(&self) -> Option<WireProtocol> {
        Some(self.protocol)
    }

    fn get_name(&self) -> &'static str {
        "J-Link"
    }

    fn speed_khz(&self) -> u32 {
        self.speed_khz
    }

    fn set_scan_chain(&mut self, scan_chain: Vec<ScanChainElement>) -> Result<(), DebugProbeError> {
        self.jtag_state.expected_scan_chain = Some(scan_chain);
        Ok(())
    }

    fn set_speed(&mut self, speed_khz: u32) -> Result<u32, DebugProbeError> {
        if speed_khz == 0 || speed_khz >= 0xffff {
            return Err(DebugProbeError::UnsupportedSpeed(speed_khz));
        }

        if let Ok(speeds) = self.read_interface_speeds() {
            tracing::debug!("Supported speeds: {:?}", speeds);

            let max_speed_khz = speeds.max_speed_hz() / 1000;

            if max_speed_khz < speed_khz {
                return Err(DebugProbeError::UnsupportedSpeed(speed_khz));
            }
        };

        if let Some(expected_speed) = SpeedConfig::khz(speed_khz as u16) {
            self.set_interface_clock_speed(expected_speed)?;
            self.speed_khz = speed_khz;
        } else {
            return Err(DebugProbeError::UnsupportedSpeed(speed_khz));
        }

        Ok(speed_khz)
    }

    fn attach(&mut self) -> Result<(), DebugProbeError> {
        tracing::debug!("Attaching to J-Link");

        tracing::debug!("Attaching with protocol '{}'", self.protocol);

        if self.caps.contains(Capability::SelectIf) {
            let jlink_interface = match self.protocol {
                WireProtocol::Swd => Interface::Swd,
                WireProtocol::Jtag => Interface::Jtag,
            };

            self.select_interface(jlink_interface)?;
        }

        // Log some information about the probe
        tracing::debug!("J-Link: Capabilities: {:?}", self.caps);
        let fw_version = self.read_firmware_version().unwrap_or_else(|_| "?".into());
        tracing::info!("J-Link: Firmware version: {}", fw_version);
        match self.read_hardware_version() {
            Ok(hw_version) => tracing::info!("J-Link: Hardware version: {}", hw_version),
            Err(_) => tracing::info!("J-Link: Hardware version: ?"),
        };

        // Check and report the target voltage.
        let target_voltage = self.get_target_voltage()?.expect("The J-Link returned None when it should only be able to return Some(f32) or an error. Please report this bug!");
        if target_voltage < crate::probe::LOW_TARGET_VOLTAGE_WARNING_THRESHOLD {
            tracing::warn!(
                "J-Link: Target voltage (VTref) is {:2.2} V. Is your target device powered?",
                target_voltage
            );
        } else {
            tracing::info!("J-Link: Target voltage: {:2.2} V", target_voltage);
        }

        match self.protocol {
            WireProtocol::Jtag => {
                // try some JTAG stuff

                tracing::debug!("Resetting JTAG chain using trst");
                self.reset_trst()?;

                self.scan_chain()?;
                self.select_target(0)?;
            }
            WireProtocol::Swd => {
                // Attaching is handled in sequence

                // We are ready to debug.
            }
        }

        self.write_cmd(&[Command::HwReset1 as u8])?;
        self.write_cmd(&[Command::HwTrst1 as u8])?;

        // Set a default speed if not already set
        if self.speed_khz == 0 {
            self.set_speed(400)?;
        }

        tracing::debug!("Attached succesfully");

        Ok(())
    }

    fn select_jtag_tap(&mut self, index: usize) -> Result<(), DebugProbeError> {
        self.select_target(index)
    }

    fn scan_chain(&self) -> Result<&[ScanChainElement], DebugProbeError> {
        match self.active_protocol() {
            Some(WireProtocol::Jtag) => {
                if let Some(ref scan_chain) = self.jtag_state.expected_scan_chain {
                    Ok(scan_chain)
                } else {
                    Ok(&[])
                }
            }
            _ => Err(DebugProbeError::InterfaceNotAvailable {
                interface_name: "JTAG",
            }),
        }
    }

    fn detach(&mut self) -> Result<(), crate::Error> {
        Ok(())
    }

    fn target_reset(&mut self) -> Result<(), DebugProbeError> {
        self.write_cmd(&[Command::ResetTarget as u8])?;
        Ok(())
    }

    fn target_reset_assert(&mut self) -> Result<(), DebugProbeError> {
        self.set_reset(false)?;
        Ok(())
    }

    fn target_reset_deassert(&mut self) -> Result<(), DebugProbeError> {
        self.set_reset(true)?;
        Ok(())
    }

    fn try_get_riscv_interface_builder<'probe>(
        &'probe mut self,
    ) -> Result<Box<dyn RiscvInterfaceBuilder<'probe> + 'probe>, DebugProbeError> {
        if self.supported_protocols.contains(&WireProtocol::Jtag) {
            self.select_protocol(WireProtocol::Jtag)?;
            Ok(Box::new(JtagDtmBuilder::new(self)))
        } else {
            Err(DebugProbeError::InterfaceNotAvailable {
                interface_name: "JTAG",
            })
        }
    }

    fn get_swo_interface(&self) -> Option<&dyn SwoAccess> {
        Some(self as _)
    }

    fn get_swo_interface_mut(&mut self) -> Option<&mut dyn SwoAccess> {
        Some(self as _)
    }

    fn has_arm_interface(&self) -> bool {
        true
    }

    fn has_riscv_interface(&self) -> bool {
        self.supported_protocols.contains(&WireProtocol::Jtag)
    }

    fn into_probe(self: Box<Self>) -> Box<dyn DebugProbe> {
        self
    }

    fn try_as_dap_probe(&mut self) -> Option<&mut dyn DapProbe> {
        Some(self)
    }

    fn try_get_arm_interface<'probe>(
        self: Box<Self>,
    ) -> Result<Box<dyn UninitializedArmProbe + 'probe>, (Box<dyn DebugProbe>, DebugProbeError)>
    {
        let uninitialized_interface = ArmCommunicationInterface::new(self, true);

        Ok(Box::new(uninitialized_interface))
    }

    fn get_target_voltage(&mut self) -> Result<Option<f32>, DebugProbeError> {
        // Convert the integer millivolts value from self.handle to volts as an f32.
        Ok(Some((self.read_target_voltage()? as f32) / 1000f32))
    }

    fn try_get_xtensa_interface<'probe>(
        &'probe mut self,
        state: &'probe mut XtensaDebugInterfaceState,
    ) -> Result<XtensaCommunicationInterface<'probe>, DebugProbeError> {
        if self.supported_protocols.contains(&WireProtocol::Jtag) {
            self.select_protocol(WireProtocol::Jtag)?;
            Ok(XtensaCommunicationInterface::new(self, state))
        } else {
            Err(DebugProbeError::InterfaceNotAvailable {
                interface_name: "JTAG",
            })
        }
    }

    fn has_xtensa_interface(&self) -> bool {
        self.supported_protocols.contains(&WireProtocol::Jtag)
    }

    fn try_into_jlink(&mut self) -> Result<&mut JLink, DebugProbeError> {
        Ok(self)
    }
}

impl RawProtocolIo for JLink {
    fn jtag_shift_tms<M>(&mut self, tms: M, tdi: bool) -> Result<(), DebugProbeError>
    where
        M: IntoIterator<Item = bool>,
    {
        assert!(
            self.protocol != WireProtocol::Swd,
            "Logic error, requested jtag_io when in SWD mode"
        );

        self.probe_statistics.report_io();

        self.shift_bits(tms, iter::repeat(tdi), iter::repeat(false))?;

        Ok(())
    }

    fn jtag_shift_tdi<I>(&mut self, tms: bool, tdi: I) -> Result<(), DebugProbeError>
    where
        I: IntoIterator<Item = bool>,
    {
        assert!(
            self.protocol != WireProtocol::Swd,
            "Logic error, requested jtag_io when in SWD mode"
        );

        self.probe_statistics.report_io();

        self.shift_bits(iter::repeat(tms), tdi, iter::repeat(false))?;

        Ok(())
    }

    fn swd_io<D, S>(&mut self, dir: D, swdio: S) -> Result<Vec<bool>, DebugProbeError>
    where
        D: IntoIterator<Item = bool>,
        S: IntoIterator<Item = bool>,
    {
        self.probe_statistics.report_io();
        self.perform_swdio_transfer(dir, swdio)
    }

    fn swj_pins(
        &mut self,
        pin_out: u32,
        pin_select: u32,
        pin_wait: u32,
    ) -> Result<u32, DebugProbeError> {
        let mut nreset = Pins(0);
        nreset.set_nreset(true);
        let nreset_mask = nreset.0 as u32;

        // If only the reset pin is selected we perform the reset.
        // If something else is selected return an error as this is not supported on J-Links.
        if pin_select == nreset_mask {
            if Pins(pin_out as u8).nreset() {
                self.target_reset_deassert()?;
            } else {
                self.target_reset_assert()?;
            }

            // Normally this would be the timeout we pass to the probe to settle the pins.
            // The J-Link is not capable of this, so we just wait for this time on the host
            // and assume it has settled until then.
            std::thread::sleep(Duration::from_micros(pin_wait as u64));

            // We signal that we cannot read the pin state.
            Ok(0xFFFF_FFFF)
        } else {
            // This is not supported for J-Links, unfortunately.
            Err(DebugProbeError::CommandNotSupportedByProbe {
                command_name: "swj_pins",
            })
        }
    }

    fn swd_settings(&self) -> &SwdSettings {
        &self.swd_settings
    }

    fn probe_statistics(&mut self) -> &mut ProbeStatistics {
        &mut self.probe_statistics
    }
}

impl RawJtagIo for JLink {
    fn state_mut(&mut self) -> &mut JtagDriverState {
        &mut self.jtag_state
    }

    fn state(&self) -> &JtagDriverState {
        &self.jtag_state
    }

    fn shift_bit(&mut self, tms: bool, tdi: bool, capture: bool) -> Result<(), DebugProbeError> {
        self.shift_jtag_bit(tms, tdi, capture)
    }

    fn read_captured_bits(&mut self) -> Result<BitVec<u8, Lsb0>, DebugProbeError> {
        self.read_captured_bits()
    }
}

impl DapProbe for JLink {}

impl SwoAccess for JLink {
    fn enable_swo(&mut self, config: &SwoConfig) -> Result<(), ArmError> {
        self.swo_config = Some(*config);
        self.swo_start(SwoMode::Uart, config.baud(), SWO_BUFFER_SIZE.into())
            .map_err(DebugProbeError::from)?;
        Ok(())
    }

    fn disable_swo(&mut self) -> Result<(), ArmError> {
        self.swo_config = None;
        self.swo_stop().map_err(DebugProbeError::from)?;
        Ok(())
    }

    fn swo_buffer_size(&mut self) -> Option<usize> {
        Some(SWO_BUFFER_SIZE.into())
    }

    fn read_swo_timeout(&mut self, timeout: Duration) -> Result<Vec<u8>, ArmError> {
        let start = Instant::now();
        let mut buf = vec![0; SWO_BUFFER_SIZE.into()];

        let poll_interval = self
            .swo_poll_interval_hint(&self.swo_config.unwrap())
            .unwrap();

        let mut bytes = vec![];
        loop {
            let data = self.swo_read(&mut buf).map_err(DebugProbeError::from)?;
            bytes.extend(data.as_ref());
            if start.elapsed() > timeout {
                break;
            }
            std::thread::sleep(poll_interval);
        }
        Ok(bytes)
    }
}

#[tracing::instrument]
fn list_jlink_devices() -> Vec<DebugProbeInfo> {
    let Ok(devices) = nusb::list_devices() else {
        return vec![];
    };

    devices
        .filter(is_jlink)
        .map(|info| {
            DebugProbeInfo::new(
                info.product_string().unwrap_or("J-Link").to_string(),
                info.vendor_id(),
                info.product_id(),
                info.serial_number().map(|s| s.to_string()),
                &JLinkFactory,
                None,
            )
        })
        .collect()
}

impl TryFrom<Interface> for WireProtocol {
    type Error = JlinkError;

    fn try_from(interface: Interface) -> Result<Self, Self::Error> {
        match interface {
            Interface::Jtag => Ok(WireProtocol::Jtag),
            Interface::Swd => Ok(WireProtocol::Swd),
            unknown_interface => Err(JlinkError::UnknownInterface(unknown_interface)),
        }
    }
}

/// A hardware version returned by [`JLink::read_hardware_version`].
///
/// Note that the reported hardware version does not allow reliable feature detection, since
/// embedded J-Link probes might return a hardware version of 1.0.0 despite supporting SWD and other
/// much newer features.
#[derive(Debug)]
struct HardwareVersion(u32);

impl HardwareVersion {
    fn from_u32(raw: u32) -> Self {
        HardwareVersion(raw)
    }

    /// Returns the type of hardware (or `None` if the hardware type is unknown).
    fn hardware_type(&self) -> Option<HardwareType> {
        Some(match (self.0 / 1000000) % 100 {
            0 => HardwareType::JLink,
            1 => HardwareType::JTrace,
            2 => HardwareType::Flasher,
            3 => HardwareType::JLinkPro,
            _ => return None,
        })
    }

    /// The major version.
    fn major(&self) -> u8 {
        // Decimal coded Decimal, cool cool
        (self.0 / 10000) as u8
    }

    /// The minor version.
    fn minor(&self) -> u8 {
        ((self.0 % 10000) / 100) as u8
    }

    /// The hardware revision.
    fn revision(&self) -> u8 {
        (self.0 % 100) as u8
    }
}

impl fmt::Display for HardwareVersion {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(hw) = self.hardware_type() {
            write!(f, "{} ", hw)?;
        }
        write!(f, "{}.{}.{}", self.major(), self.minor(), self.revision())
    }
}

/// The hardware/product type of the device.
#[non_exhaustive]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum HardwareType {
    JLink,
    JTrace,
    Flasher,
    JLinkPro,
}

impl fmt::Display for HardwareType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            HardwareType::JLink => "J-Link",
            HardwareType::JTrace => "J-Trace",
            HardwareType::Flasher => "J-Flash",
            HardwareType::JLinkPro => "J-Link Pro",
        })
    }
}

const VID_SEGGER: u16 = 0x1366;

// Information about the product IDs is taken from the udev rules file provided by Segger.

/// Product IDS for J-Link devices using the old format
///
/// 0x106 is not included on purpose, it is supposed to be serial (CDC) only
const LEGACY_JLINK_IDS: &[u16] = &[0x0101, 0x0102, 0x0103, 0x0104, 0x0105, 0x0107, 0x108];

/// Flag indicating that the product ID is a J-Link product ID, using the new format
const PID_JLINK_FLAG: u16 = 0x1000;

/// Indicates that the J-Link is using the (legacy) Segger driver.
const PID_JLINK_SEGGER_DRV_FLAG: u16 = 1 << 4;

/// Indicates that the J-Link is using the WinUSB driver.
const PID_JLINK_WINUSB_DRV_FLAG: u16 = 1 << 5;

fn is_jlink_product_id(product_id: u16) -> bool {
    let old_product_id = LEGACY_JLINK_IDS.contains(&product_id);

    let new_product_id = (product_id & PID_JLINK_FLAG) != 0;

    let jlink_using_segger_driver = (product_id & PID_JLINK_SEGGER_DRV_FLAG) != 0;

    let jlink_using_winusb_driver = (product_id & PID_JLINK_WINUSB_DRV_FLAG) != 0;

    old_product_id || (new_product_id && (jlink_using_segger_driver || jlink_using_winusb_driver))
}

fn is_jlink(info: &DeviceInfo) -> bool {
    let matching_vendor_id = info.vendor_id() == VID_SEGGER;

    matching_vendor_id && is_jlink_product_id(info.product_id())
}

#[cfg(test)]
mod test {
    #[test]
    fn jlink_pid_cmsisdap() {
        // J-Link devices configured as CMSIS-DAP should not be detected as J-Link devices.
        assert!(!super::is_jlink_product_id(0x1008));
    }

    #[test]
    fn jlink_pid_segger_driver() {
        // J-Link device using the legacy Segger driver.
        assert!(super::is_jlink_product_id(0x1059));
    }
}