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
//! The DDC bus: one trait for every target on it.
//!
//! An HDMI connector carries one DDC line, an I²C bus, shared by several targets: the
//! sink's EDID at [`EDID`] (with the E-DDC segment pointer at [`SEGMENT_POINTER`]), its
//! DisplayID at [`DISPLAYID`], SCDC at [`SCDC`], HDCP at [`HDCP_PRIMARY`] and
//! [`HDCP_SECONDARY`], and DDC/CI at [`DDC_CI`]. [`DdcBus`] is the contract for that bus;
//! code that talks to one target (SCDC through `ScdcTransport`, an EDID reader, an HDCP
//! implementation) runs over it.
//!
//! The hardware below differs widely: general I²C controllers, DDC masters driven by
//! segment, address and offset registers, engines that can only read EDID, controllers
//! shared with firmware. [`DdcCapabilities`] says what an implementation can do, and an
//! operation it cannot do fails with [`DdcErrorKind::Unsupported`] before it reaches the bus.
use Infallible;
use Deref;
/// The E-DDC segment pointer (8-bit address 60h), written before an EDID or DisplayID read
/// of segment 1 or higher (VESA E-DDC 1.3, 2.2.5).
pub const SEGMENT_POINTER: u8 = 0x30;
/// HDCP's primary link port (8-bit address 74h; VESA E-DDC 1.3, Table 2-2; Linux's
/// `DRM_HDCP_DDC_ADDR` and amdgpu's primary port, torvalds/linux `e774d5f1b`,
/// `include/drm/display/drm_hdcp.h:30`, `drivers/gpu/drm/amd/display/dc/hdcp/hdcp_msg.c:128`).
pub const HDCP_PRIMARY: u8 = 0x3A;
/// HDCP's secondary link port (8-bit address 76h; VESA E-DDC 1.3, Table 2-2; amdgpu's
/// secondary port, torvalds/linux `e774d5f1b`, `drivers/gpu/drm/amd/display/dc/hdcp/hdcp_msg.c:129`).
pub const HDCP_SECONDARY: u8 = 0x3B;
/// DDC/CI, the display's command interface (8-bit address 6Eh; VESA DDC/CI 1.1, 2.3).
pub const DDC_CI: u8 = 0x37;
/// The EDID structure (8-bit address A0h; VESA E-DDC 1.3, 2.2.1).
pub const EDID: u8 = 0x50;
/// The DisplayID structure (8-bit address A4h; VESA E-DDC 1.3, 2.2.1).
pub const DISPLAYID: u8 = 0x52;
/// SCDC, the status and control data channel (8-bit address A8h; Linux's SCDC helper and
/// amdgpu, torvalds/linux `e774d5f1b`, `drivers/gpu/drm/display/drm_scdc_helper.c:56`,
/// `drivers/gpu/drm/amd/display/dc/dc_hdmi_types.h:58`).
pub const SCDC: u8 = 0x54;
/// The highest E-DDC segment (VESA E-DDC 1.3, Tables 5-1 and 5-2: 00h–7Fh).
pub const MAX_SEGMENT: u8 = 0x7F;
/// Whether `addr` is a 7-bit address a [`DdcBus`] may be asked to reach: not above 0x7F,
/// and outside I²C's reserved ranges 0x00–0x07 and 0x78–0x7F.
///
/// An implementation returns [`DdcErrorKind::Unsupported`], without bus activity, for an
/// address this rejects.
pub const
/// Access to the DDC bus of one connector.
///
/// # Operations
///
/// | Method | On the wire |
/// |---|---|
/// | [`write`](Self::write) | START, address + W, `bytes`, STOP |
/// | [`write_read`](Self::write_read) | START, address + W, `bytes`, repeated START, address + R, `buf.len()` bytes, STOP |
/// | [`read`](Self::read) | START, address + R, `buf.len()` bytes, STOP |
/// | [`read_segmented`](Self::read_segmented) | an E-DDC read: for segment 1 and higher a write of the segment to [`SEGMENT_POINTER`], then as `write_read` with the offset to the target, with no STOP in between; for segment 0, no write to the pointer |
///
/// Register-style targets (SCDC, HDCP) take a register offset as the first byte of
/// `bytes`.
///
/// # One call, one transaction
///
/// [`write`](Self::write), [`write_read`](Self::write_read) and [`read`](Self::read) each
/// run as one transaction or return an error: an implementation never splits them into
/// several transfers. Where the hardware does so anyway (one byte per hardware
/// operation), the implementation says so through [`DdcCapabilities::splits_reads`].
///
/// [`read_segmented`](Self::read_segmented) may be split into several E-DDC transactions,
/// each writing the segment pointer and the offset again (the display resets the pointer
/// on STOP or NACK: VESA E-DDC 1.3, 2.2.5, 8.1.1); an implementation that splits reports
/// its piece size through [`DdcCapabilities::segmented_piece`]. A split read can span a
/// hotplug and return a block made of two EDIDs: the block checksum is the check.
///
/// Between two calls, another user's transaction may run: nothing here holds the bus
/// across calls.
///
/// # Sharing
///
/// Every method takes `&self`, so several users (an EDID reader, an SCDC client, an HDCP
/// implementation) share one bus through references or shared pointers. The
/// implementation serializes concurrent calls, one transaction at a time, and never panics
/// doing so; one that cannot serialize returns an error for the second call and says so in
/// its documentation.
///
/// # Arguments
///
/// Checked before any bus activity:
///
/// - `addr` outside [`is_usable_address`]: [`DdcErrorKind::Unsupported`].
/// - An empty `buf` (in `read`, `write_read`, `read_segmented`): `Ok`, no bus activity.
/// - `write_read` with empty `bytes` is a [`read`](Self::read).
/// - `write` with empty `bytes`: [`DdcErrorKind::Unsupported`] (a zero-length write is
/// I²C's address probe, which many adapters reject).
/// - `read_segmented` with `segment` above [`MAX_SEGMENT`], or `offset + buf.len()` above
/// 256: [`DdcErrorKind::Unsupported`].
/// - Anything [`capabilities`](Self::capabilities) rules out (a length, an address, a
/// target, a segment): [`DdcErrorKind::Unsupported`].
///
/// # E-DDC
///
/// For segment 1 and higher, a missing acknowledge of the pointer value fails the read: the
/// display has no such segment (VESA E-DDC 1.3, 6.4, 6.5). For segment 0 an implementation
/// does not need to write the pointer, and a display without one still answers (8.1.2);
/// hardware whose engine writes it anyway conforms all the same. A read across the
/// 128-byte block boundary is allowed, but whether the display continues past it is the
/// display's (2.2.6).
///
/// # Errors
///
/// Every error carries a [`DdcErrorKind`]. An implementation adds no retries of its own;
/// retries in the layer below it (an OS bus layer, a fallback in the driver) are named in
/// its documentation, since they multiply with the caller's. After an error the bus is
/// usable for the next call as far as the hardware allows: the implementation recovers it
/// (clock pulses, an abort, a reset) or says what is needed. The contents of `buf` after
/// an error are unspecified.
///
/// # Example
///
/// A bus that reaches nothing but an EDID store, as an EDID-only engine does:
///
/// ```
/// use hdmi_hal::ddc::{DdcBus, DdcCapabilities, DdcError, DdcErrorKind, EddcTarget};
///
/// #[derive(Debug)]
/// struct Unsupported;
///
/// impl DdcError for Unsupported {
/// fn kind(&self) -> DdcErrorKind {
/// DdcErrorKind::Unsupported
/// }
/// }
///
/// /// Two blocks of EDID, segment 0 only.
/// struct EdidEngine {
/// edid: [u8; 256],
/// }
///
/// impl DdcBus for EdidEngine {
/// type Error = Unsupported;
///
/// fn capabilities(&self) -> DdcCapabilities {
/// DdcCapabilities::none()
/// .with_eddc_target(EddcTarget::Edid, true)
/// .with_max_segment(Some(0))
/// }
///
/// fn write(&self, _addr: u8, _bytes: &[u8]) -> Result<(), Unsupported> {
/// Err(Unsupported)
/// }
///
/// fn write_read(&self, _addr: u8, _bytes: &[u8], _buf: &mut [u8]) -> Result<(), Unsupported> {
/// Err(Unsupported)
/// }
///
/// fn read(&self, _addr: u8, _buf: &mut [u8]) -> Result<(), Unsupported> {
/// Err(Unsupported)
/// }
///
/// fn read_segmented(
/// &self,
/// target: EddcTarget,
/// segment: u8,
/// offset: u8,
/// buf: &mut [u8],
/// ) -> Result<(), Unsupported> {
/// let start = usize::from(offset);
/// if target != EddcTarget::Edid || segment != 0 || start + buf.len() > 256 {
/// return Err(Unsupported);
/// }
/// buf.copy_from_slice(&self.edid[start..start + buf.len()]);
/// Ok(())
/// }
/// }
///
/// let bus = EdidEngine { edid: [0; 256] };
/// let mut header = [0u8; 8];
/// bus.read_segmented(EddcTarget::Edid, 0, 0, &mut header).unwrap();
/// assert!(!bus.capabilities().reaches(hdmi_hal::ddc::SCDC));
/// ```
/// A handle to a [`DdcBus`]: what code that shares the bus holds.
///
/// Implemented for every handle that dereferences to a bus (`&B`, `&'static B`, `Rc<B>`,
/// `Arc<B>`), with no `alloc` needed. A handle that is itself a cheap, `Copy` view of a bus
/// (a test double's, for instance) implements it for itself, so it is passed by value:
///
/// ```
/// use hdmi_hal::ddc::{DdcBus, DdcCapabilities, DdcHandle, EddcTarget};
///
/// /// A `Copy` view of some bus.
/// #[derive(Clone, Copy)]
/// struct View;
///
/// impl DdcBus for View {
/// type Error = hdmi_hal::ddc::DdcErrorKind;
/// fn capabilities(&self) -> DdcCapabilities {
/// DdcCapabilities::none()
/// }
/// fn write(&self, _: u8, _: &[u8]) -> Result<(), Self::Error> {
/// Ok(())
/// }
/// fn write_read(&self, _: u8, _: &[u8], _: &mut [u8]) -> Result<(), Self::Error> {
/// Ok(())
/// }
/// fn read(&self, _: u8, _: &mut [u8]) -> Result<(), Self::Error> {
/// Ok(())
/// }
/// fn read_segmented(&self, _: EddcTarget, _: u8, _: u8, _: &mut [u8]) -> Result<(), Self::Error> {
/// Ok(())
/// }
/// }
///
/// impl DdcHandle for View {
/// type Bus = Self;
/// fn bus(&self) -> &Self {
/// self
/// }
/// }
/// ```
/// An error of a [`DdcBus`], classified.
///
/// Implemented for [`Infallible`] and `()` (kind [`DdcErrorKind::Other`]), so test doubles
/// can use either, and for [`DdcErrorKind`] itself.
/// The kind of a [`DdcBus`] failure.
///
/// What a kind means to a caller: [`NoAcknowledge`](Self::NoAcknowledge) usually means
/// nothing answers at that address, not always (a passive adapter may refuse a first
/// transaction); [`Timeout`](Self::Timeout), [`Busy`](Self::Busy) and
/// [`Other`](Self::Other) may be transient; [`Unsupported`](Self::Unsupported) and
/// [`Detached`](Self::Detached) are not.
/// Which byte of a transaction was not acknowledged.
/// The structures an E-DDC read reaches, each with its own address and the shared segment
/// pointer (VESA E-DDC 1.3, 2.2.1, 2.2.4).
/// A kind is an error of its own kind, for an implementation with nothing more to report
/// (as embedded-hal's `ErrorKind` is its own `Error`).
/// The addresses [`is_usable_address`] accepts, as a set (bit `n` for address `n`).
const USABLE_ADDRESSES: u128 = ;
/// What a [`DdcBus`] implementation can do.
///
/// It may under-state, never over-state: what an implementation cannot vouch for, it
/// reports as unavailable, and the operation returns [`DdcErrorKind::Unsupported`] before
/// it reaches the bus.
///
/// Built from [`general`](Self::general) or [`none`](Self::none) and the `with_*`
/// setters, all `const`:
///
/// ```
/// use hdmi_hal::ddc::{DdcCapabilities, DDC_CI, SCDC};
///
/// // An SMBus-style adapter: one-byte offsets, 32-byte block reads, single-byte plain reads.
/// const SMBUS: DdcCapabilities = DdcCapabilities::general()
/// .with_write_max(33)
/// .with_write_read_max(1, 32)
/// .with_read_max(1)
/// .with_max_segment(Some(0))
/// .with_segmented_piece(Some(32));
///
/// assert_eq!(SMBUS.write_read_max(), (1, 32));
/// assert!(SMBUS.reaches(SCDC));
///
/// // A controller that cannot reach DDC/CI.
/// let caps = DdcCapabilities::general().with_address_reachable(DDC_CI, false);
/// assert!(!caps.reaches(DDC_CI));
/// ```