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
use std::fmt;
use std::io;
use std::ptr;
use std::result;

use libc::{self, c_int, c_ulong, ioctl};

#[cfg(target_env = "gnu")]
type IoctlLong = libc::c_ulong;
#[cfg(target_env = "musl")]
type IoctlLong = c_int;

pub type Result<T> = result::Result<T, io::Error>;

// Based on i2c.h, i2c-dev.c, i2c-dev.h and the documentation at https://www.kernel.org/doc/Documentation/i2c
// and http://smbus.org/specs/SMBus_3_1_20180319.pdf

// Capabilities returned by REQ_FUNCS
const FUNC_I2C: c_ulong = 0x01;
const FUNC_10BIT_ADDR: c_ulong = 0x02;
const FUNC_PROTOCOL_MANGLING: c_ulong = 0x04;
const FUNC_SMBUS_PEC: c_ulong = 0x08;
const FUNC_NOSTART: c_ulong = 0x10;
const FUNC_SLAVE: c_ulong = 0x20;
const FUNC_SMBUS_BLOCK_PROC_CALL: c_ulong = 0x8000;
const FUNC_SMBUS_QUICK: c_ulong = 0x01_0000;
const FUNC_SMBUS_READ_BYTE: c_ulong = 0x02_0000;
const FUNC_SMBUS_WRITE_BYTE: c_ulong = 0x04_0000;
const FUNC_SMBUS_READ_BYTE_DATA: c_ulong = 0x08_0000;
const FUNC_SMBUS_WRITE_BYTE_DATA: c_ulong = 0x10_0000;
const FUNC_SMBUS_READ_WORD_DATA: c_ulong = 0x20_0000;
const FUNC_SMBUS_WRITE_WORD_DATA: c_ulong = 0x40_0000;
const FUNC_SMBUS_PROC_CALL: c_ulong = 0x80_0000;
const FUNC_SMBUS_READ_BLOCK_DATA: c_ulong = 0x0100_0000;
const FUNC_SMBUS_WRITE_BLOCK_DATA: c_ulong = 0x0200_0000;
const FUNC_SMBUS_READ_I2C_BLOCK: c_ulong = 0x0400_0000;
const FUNC_SMBUS_WRITE_I2C_BLOCK: c_ulong = 0x0800_0000;
const FUNC_SMBUS_HOST_NOTIFY: c_ulong = 0x1000_0000;

/// Lists the features supported by the underlying drivers.
#[derive(PartialEq, Eq, Copy, Clone)]
pub struct Capabilities {
    funcs: c_ulong,
}

impl Capabilities {
    /// Constructs a new `Capabilities`.
    ///
    /// `Capabilities` indicates which I2C features and SMBus protocols
    /// are supported by the underlying drivers.
    fn new(funcs: c_ulong) -> Capabilities {
        Capabilities { funcs }
    }

    pub(crate) fn i2c(self) -> bool {
        (self.funcs & FUNC_I2C) > 0
    }

    pub(crate) fn slave(self) -> bool {
        (self.funcs & FUNC_SLAVE) > 0
    }

    /// Indicates whether 10-bit addresses are supported.
    pub fn addr_10bit(self) -> bool {
        (self.funcs & FUNC_10BIT_ADDR) > 0
    }

    /// Indicates whether I2C Block Read is supported.
    pub fn i2c_block_read(self) -> bool {
        (self.funcs & FUNC_SMBUS_READ_I2C_BLOCK) > 0
    }

    /// Indicates whether I2C Block Write is supported.
    pub fn i2c_block_write(self) -> bool {
        (self.funcs & FUNC_SMBUS_WRITE_I2C_BLOCK) > 0
    }

    /// Indicates whether protocol mangling is supported.
    pub(crate) fn protocol_mangling(self) -> bool {
        (self.funcs & FUNC_PROTOCOL_MANGLING) > 0
    }

    /// Indicates whether the NOSTART flag is supported.
    pub(crate) fn nostart(self) -> bool {
        (self.funcs & FUNC_NOSTART) > 0
    }

    /// Indicates whether SMBus Quick Command is supported.
    pub fn smbus_quick_command(self) -> bool {
        (self.funcs & FUNC_SMBUS_QUICK) > 0
    }

    /// Indicates whether SMBus Receive Byte is supported.
    pub fn smbus_receive_byte(self) -> bool {
        (self.funcs & FUNC_SMBUS_READ_BYTE) > 0
    }

    /// Indicates whether SMBus Send Byte is supported.
    pub fn smbus_send_byte(self) -> bool {
        (self.funcs & FUNC_SMBUS_WRITE_BYTE) > 0
    }

    /// Indicates whether SMBus Read Byte is supported.
    pub fn smbus_read_byte(self) -> bool {
        (self.funcs & FUNC_SMBUS_READ_BYTE_DATA) > 0
    }

    /// Indicates whether SMBus Write Byte is supported.
    pub fn smbus_write_byte(self) -> bool {
        (self.funcs & FUNC_SMBUS_WRITE_BYTE_DATA) > 0
    }

    /// Indicates whether SMBus Read Word is supported.
    pub fn smbus_read_word(self) -> bool {
        (self.funcs & FUNC_SMBUS_READ_WORD_DATA) > 0
    }

    /// Indicates whether SMBus Write Word is supported.
    pub fn smbus_write_word(self) -> bool {
        (self.funcs & FUNC_SMBUS_WRITE_WORD_DATA) > 0
    }

    /// Indicates whether SMBus Process Call is supported.
    pub fn smbus_process_call(self) -> bool {
        (self.funcs & FUNC_SMBUS_PROC_CALL) > 0
    }

    /// Indicates whether SMBus Block Read is supported.
    pub fn smbus_block_read(self) -> bool {
        (self.funcs & FUNC_SMBUS_READ_BLOCK_DATA) > 0
    }

    /// Indicates whether SMBus Block Write is supported.
    pub fn smbus_block_write(self) -> bool {
        (self.funcs & FUNC_SMBUS_WRITE_BLOCK_DATA) > 0
    }

    /// Indicates whether SMBus Block Process Call is supported.
    pub fn smbus_block_process_call(self) -> bool {
        (self.funcs & FUNC_SMBUS_BLOCK_PROC_CALL) > 0
    }

    /// Indicates whether SMBus Packet Error Checking is supported.
    pub fn smbus_pec(self) -> bool {
        (self.funcs & FUNC_SMBUS_PEC) > 0
    }

    /// Indicates whether SMBus Host Notify is supported.
    pub fn smbus_host_notify(self) -> bool {
        (self.funcs & FUNC_SMBUS_HOST_NOTIFY) > 0
    }
}

impl fmt::Debug for Capabilities {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Capabilities")
            .field("addr_10bit", &self.addr_10bit())
            .field("i2c_block_read", &self.i2c_block_read())
            .field("i2c_block_write", &self.i2c_block_write())
            .field("smbus_quick_command", &self.smbus_quick_command())
            .field("smbus_receive_byte", &self.smbus_receive_byte())
            .field("smbus_send_byte", &self.smbus_send_byte())
            .field("smbus_read_byte", &self.smbus_read_byte())
            .field("smbus_write_byte", &self.smbus_write_byte())
            .field("smbus_read_word", &self.smbus_read_word())
            .field("smbus_write_word", &self.smbus_write_word())
            .field("smbus_process_call", &self.smbus_process_call())
            .field("smbus_block_read", &self.smbus_block_read())
            .field("smbus_block_write", &self.smbus_block_write())
            .field("smbus_block_process_call", &self.smbus_block_process_call())
            .field("smbus_pec", &self.smbus_pec())
            .field("smbus_host_notify", &self.smbus_host_notify())
            .finish()
    }
}

// ioctl() requests supported by i2cdev
const REQ_RETRIES: IoctlLong = 0x0701; // How many retries when waiting for an ACK
const REQ_TIMEOUT: IoctlLong = 0x0702; // Timeout in 10ms units
const REQ_SLAVE: IoctlLong = 0x0706; // Set slave address
const REQ_SLAVE_FORCE: IoctlLong = 0x0703; // Set slave address, even if it's already in use by a driver
const REQ_TENBIT: IoctlLong = 0x0704; // Use 10-bit slave addresses
const REQ_FUNCS: IoctlLong = 0x0705; // Read I2C bus capabilities
const REQ_RDWR: IoctlLong = 0x0707; // Combined read/write transfer with a single STOP
const REQ_PEC: IoctlLong = 0x0708; // SMBus: Use Packet Error Checking
const REQ_SMBUS: IoctlLong = 0x0720; // SMBus: Transfer data

// NOTE: REQ_RETRIES - Supported in i2cdev, but not used in the underlying drivers
// NOTE: REQ_RDWR - Only a single read operation is supported as the final message (see i2c-bcm2835.c)

const RDWR_FLAG_RD: u16 = 0x0001; // Read operation
const RDWR_FLAG_TEN: u16 = 0x0010; // 10-bit slave address

const RDWR_MSG_MAX: usize = 42; // Maximum messages per RDWR operation
const SMBUS_BLOCK_MAX: usize = 32; // Maximum bytes per block transfer

// SMBus read or write request
#[derive(Debug, PartialEq, Copy, Clone)]
enum SmbusReadWrite {
    Read = 1,
    Write = 0,
}

// Size/Type identifiers for the data contained in SmbusBuffer
#[derive(Debug, PartialEq, Copy, Clone)]
enum SmbusSize {
    Quick = 0,
    Byte = 1,
    ByteData = 2,
    WordData = 3,
    ProcCall = 4,
    BlockData = 5,
    I2cBlockData = 8,
}

// Holds data transferred by REQ_SMBUS requests. Data can either consist of a
// single byte, a 16-bit word, or a block, where the first byte contains the length,
// followed by up to 32 bytes of data, with the final byte used as padding.
#[derive(Copy, Clone)]
#[repr(C)]
struct SmbusBuffer {
    data: [u8; SMBUS_BLOCK_MAX + 2],
}

impl SmbusBuffer {
    pub fn new() -> SmbusBuffer {
        SmbusBuffer {
            data: [0u8; SMBUS_BLOCK_MAX + 2],
        }
    }

    pub fn with_byte(value: u8) -> SmbusBuffer {
        let mut buffer = SmbusBuffer {
            data: [0u8; SMBUS_BLOCK_MAX + 2],
        };

        buffer.data[0] = value;

        buffer
    }

    pub fn with_word(value: u16) -> SmbusBuffer {
        let mut buffer = SmbusBuffer {
            data: [0u8; SMBUS_BLOCK_MAX + 2],
        };

        // Low byte is sent first (SMBus 3.1 spec @ 6.5.4)
        buffer.data[0] = (value & 0xFF) as u8;
        buffer.data[1] = (value >> 8) as u8;

        buffer
    }

    pub fn with_buffer(value: &[u8]) -> SmbusBuffer {
        let mut buffer = SmbusBuffer {
            data: [0u8; SMBUS_BLOCK_MAX + 2],
        };

        buffer.data[0] = if value.len() > SMBUS_BLOCK_MAX {
            buffer.data[1..=SMBUS_BLOCK_MAX].copy_from_slice(&value[..SMBUS_BLOCK_MAX]);
            SMBUS_BLOCK_MAX as u8
        } else {
            buffer.data[1..=value.len()].copy_from_slice(value);
            value.len() as u8
        };

        buffer
    }
}

// Specifies SMBus request parameters
#[repr(C)]
struct SmbusRequest {
    // Read (1) or write (0) request.
    read_write: u8,
    // User-specified 8-bit command value.
    command: u8,
    // Request type identifier.
    size: u32,
    // Pointer to buffer, or 0.
    data: *mut SmbusBuffer,
}

fn smbus_request(
    fd: c_int,
    read_write: SmbusReadWrite,
    command: u8,
    size: SmbusSize,
    data: Option<&mut SmbusBuffer>,
) -> Result<()> {
    let mut request = SmbusRequest {
        read_write: read_write as u8,
        command,
        size: size as u32,
        data: if let Some(buffer) = data {
            buffer
        } else {
            ptr::null_mut()
        },
    };

    parse_retval!(unsafe { ioctl(fd, REQ_SMBUS, &mut request) })?;

    Ok(())
}

pub fn smbus_quick_command(fd: c_int, value: bool) -> Result<()> {
    // Quick Command uses the read_write field, instead of the data buffer
    smbus_request(
        fd,
        if value {
            SmbusReadWrite::Read
        } else {
            SmbusReadWrite::Write
        },
        0,
        SmbusSize::Quick,
        None,
    )
}

pub fn smbus_receive_byte(fd: c_int) -> Result<u8> {
    let mut buffer = SmbusBuffer::new();
    smbus_request(
        fd,
        SmbusReadWrite::Read,
        0,
        SmbusSize::Byte,
        Some(&mut buffer),
    )?;

    Ok(buffer.data[0])
}

pub fn smbus_send_byte(fd: c_int, value: u8) -> Result<()> {
    // Send Byte uses the command field, instead of the data buffer
    smbus_request(fd, SmbusReadWrite::Write, value, SmbusSize::Byte, None)
}

pub fn smbus_read_byte(fd: c_int, command: u8) -> Result<u8> {
    let mut buffer = SmbusBuffer::new();
    smbus_request(
        fd,
        SmbusReadWrite::Read,
        command,
        SmbusSize::ByteData,
        Some(&mut buffer),
    )?;

    Ok(buffer.data[0])
}

pub fn smbus_read_word(fd: c_int, command: u8) -> Result<u16> {
    let mut buffer = SmbusBuffer::new();
    smbus_request(
        fd,
        SmbusReadWrite::Read,
        command,
        SmbusSize::WordData,
        Some(&mut buffer),
    )?;

    // Low byte is received first (SMBus 3.1 spec @ 6.5.5)
    Ok(u16::from(buffer.data[0]) | (u16::from(buffer.data[1]) << 8))
}

pub fn smbus_write_byte(fd: c_int, command: u8, value: u8) -> Result<()> {
    let mut buffer = SmbusBuffer::with_byte(value);
    smbus_request(
        fd,
        SmbusReadWrite::Write,
        command,
        SmbusSize::ByteData,
        Some(&mut buffer),
    )
}

pub fn smbus_write_word(fd: c_int, command: u8, value: u16) -> Result<()> {
    let mut buffer = SmbusBuffer::with_word(value);
    smbus_request(
        fd,
        SmbusReadWrite::Write,
        command,
        SmbusSize::WordData,
        Some(&mut buffer),
    )
}

pub fn smbus_process_call(fd: c_int, command: u8, value: u16) -> Result<u16> {
    let mut buffer = SmbusBuffer::with_word(value);
    smbus_request(
        fd,
        SmbusReadWrite::Write,
        command,
        SmbusSize::ProcCall,
        Some(&mut buffer),
    )?;

    // Low byte is received first (SMBus 3.1 spec @ 6.5.6)
    Ok(u16::from(buffer.data[0]) | (u16::from(buffer.data[1]) << 8))
}

pub fn smbus_block_read(fd: c_int, command: u8, value: &mut [u8]) -> Result<usize> {
    let mut buffer = SmbusBuffer::new();
    smbus_request(
        fd,
        SmbusReadWrite::Read,
        command,
        SmbusSize::BlockData,
        Some(&mut buffer),
    )?;

    // Verify the length in case we're receiving corrupted data
    let incoming_length = if buffer.data[0] as usize > SMBUS_BLOCK_MAX {
        SMBUS_BLOCK_MAX
    } else {
        buffer.data[0] as usize
    };

    // Make sure the incoming data fits in the value buffer
    let value_length = value.len();
    if incoming_length > value_length {
        value.copy_from_slice(&buffer.data[1..=value_length]);
    } else {
        value[..incoming_length].copy_from_slice(&buffer.data[1..=incoming_length]);
    }

    Ok(incoming_length)
}

pub fn smbus_block_write(fd: c_int, command: u8, value: &[u8]) -> Result<()> {
    let mut buffer = SmbusBuffer::with_buffer(value);
    smbus_request(
        fd,
        SmbusReadWrite::Write,
        command,
        SmbusSize::BlockData,
        Some(&mut buffer),
    )
}

pub fn i2c_block_read(fd: c_int, command: u8, value: &mut [u8]) -> Result<()> {
    let mut buffer = SmbusBuffer::new();
    buffer.data[0] = if value.len() > SMBUS_BLOCK_MAX {
        SMBUS_BLOCK_MAX as u8
    } else {
        value.len() as u8
    };

    smbus_request(
        fd,
        SmbusReadWrite::Read,
        command,
        SmbusSize::I2cBlockData,
        Some(&mut buffer),
    )?;

    value[..buffer.data[0] as usize].copy_from_slice(&buffer.data[1..=buffer.data[0] as usize]);

    Ok(())
}

pub fn i2c_block_write(fd: c_int, command: u8, value: &[u8]) -> Result<()> {
    let mut buffer = SmbusBuffer::with_buffer(value);
    smbus_request(
        fd,
        SmbusReadWrite::Write,
        command,
        SmbusSize::I2cBlockData,
        Some(&mut buffer),
    )
}

// Specifies RDWR segment parameters
#[repr(C)]
#[derive(Debug, PartialEq, Copy, Clone)]
struct RdwrSegment {
    // Slave address
    addr: u16,
    // Segment flags
    flags: u16,
    // Buffer length
    len: u16,
    // Pointer to buffer
    data: usize,
}

// Specifies RWDR request parameters
#[repr(C)]
#[derive(Debug, PartialEq, Copy, Clone)]
struct RdwrRequest {
    // Pointer to an array of segments
    segments: *mut [RdwrSegment],
    // Number of segments
    nmsgs: u32,
}

pub fn i2c_write_read(
    fd: c_int,
    address: u16,
    addr_10bit: bool,
    write_buffer: &[u8],
    read_buffer: &mut [u8],
) -> Result<()> {
    // 0 length buffers may cause issues
    if write_buffer.is_empty() || read_buffer.is_empty() {
        return Ok(());
    }

    let segment_write = RdwrSegment {
        addr: address,
        flags: if addr_10bit { RDWR_FLAG_TEN } else { 0 },
        len: write_buffer.len() as u16,
        data: write_buffer.as_ptr() as usize,
    };

    let segment_read = RdwrSegment {
        addr: address,
        flags: if addr_10bit {
            RDWR_FLAG_RD | RDWR_FLAG_TEN
        } else {
            RDWR_FLAG_RD
        },
        len: read_buffer.len() as u16,
        data: read_buffer.as_mut_ptr() as usize,
    };

    let mut segments: [RdwrSegment; 2] = [segment_write, segment_read];
    let mut request = RdwrRequest {
        segments: &mut segments,
        nmsgs: 2,
    };

    parse_retval!(unsafe { ioctl(fd, REQ_RDWR, &mut request) })?;

    Ok(())
}

pub fn set_slave_address(fd: c_int, value: c_ulong) -> Result<()> {
    parse_retval!(unsafe { ioctl(fd, REQ_SLAVE, value) })?;

    Ok(())
}

pub fn set_addr_10bit(fd: c_int, value: c_ulong) -> Result<()> {
    parse_retval!(unsafe { ioctl(fd, REQ_TENBIT, value) })?;

    Ok(())
}

pub fn set_pec(fd: c_int, value: c_ulong) -> Result<()> {
    parse_retval!(unsafe { ioctl(fd, REQ_PEC, value) })?;

    Ok(())
}

pub fn set_timeout(fd: c_int, value: c_ulong) -> Result<()> {
    // Timeout is specified in units of 10ms
    let timeout: c_ulong = if value > 0 && value < 10 {
        1
    } else {
        value / 10
    };

    parse_retval!(unsafe { ioctl(fd, REQ_TIMEOUT, timeout) })?;

    Ok(())
}

pub fn set_retries(fd: c_int, value: c_ulong) -> Result<()> {
    // Number of retries on arbitration loss
    parse_retval!(unsafe { ioctl(fd, REQ_RETRIES, value) })?;

    Ok(())
}

pub fn funcs(fd: c_int) -> Result<Capabilities> {
    let mut funcs: c_ulong = 0;

    parse_retval!(unsafe { ioctl(fd, REQ_FUNCS, &mut funcs) })?;

    Ok(Capabilities::new(funcs))
}