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
use std::mem;

pub use {ca_descr_type::*, ca_slot_flags::*, ca_slot_type::*};

mod ca_slot_type {
    /// CI high level interface
    pub const CA_CI: u32 = 1;
    /// CI link layer level interface
    pub const CA_CI_LINK: u32 = 2;
    /// CI physical layer level interface
    pub const CA_CI_PHYS: u32 = 4;
    /// built-in descrambler
    pub const CA_DESCR: u32 = 8;
    /// simple smart card interface
    pub const CA_SC: u32 = 128;
}

mod ca_slot_flags {
    pub const CA_CI_MODULE_NOT_FOUND: u32 = 0;
    /// module (or card) inserted
    pub const CA_CI_MODULE_PRESENT: u32 = 1;
    /// module is ready for usage
    pub const CA_CI_MODULE_READY: u32 = 2;
}

/// CA slot interface types and info
#[repr(C)]
#[derive(Default, Debug)]
pub struct CaSlotInfo {
    /// slot number
    pub slot_num: u32,
    /// slot type - ca_slot_type
    pub slot_type: u32,
    /// flags applicable to the slot - ca_slot_flags
    pub flags: u32,
}

mod ca_descr_type {
    /// European Common Descrambler (ECD) hardware
    pub const CA_ECD: u32 = 1;
    /// Videoguard (NDS) hardware
    pub const CA_NDS: u32 = 2;
    /// Distributed Sample Scrambling (DSS) hardware
    pub const CA_DSS: u32 = 4;
}

/// descrambler types and info
#[repr(C)]
#[derive(Default, Debug)]
pub struct CaDescrInfo {
    /// number of available descramblers (keys)
    pub descr_num: u32,
    /// type of supported scrambling system - ca_descr_type
    pub descr_type: u32,
}

/// CA slot interface capabilities
#[repr(C)]
#[derive(Default, Debug)]
pub struct CaCaps {
    /// total number of CA card and module slots
    pub slot_num: u32,
    /// bitmap with all supported types as defined at ca_slot_info
    pub slot_type: u32,
    /// total number of descrambler slots (keys)
    pub descr_num: u32,
    /// bitmap with all supported types as defined at ca_descr_info
    pub descr_type: u32,
}

/// a message to/from a CI-CAM
#[repr(C)]
#[derive(Debug)]
pub struct CaMsg {
    /// unused
    index: u32,
    /// unused
    typ: u32,
    /// length of the message
    pub length: u32,
    /// message
    pub msg: [u8; 256],
}

impl Default for CaMsg {
    #[inline]
    fn default() -> Self {
        unsafe { mem::zeroed::<Self>() }
    }
}

/// CA descrambler control words info
#[repr(C)]
#[derive(Default, Debug)]
pub struct CaDescr {
    /// CA Descrambler slot
    pub index: u32,
    /// control words parity, where 0 means even and 1 means odd
    pub parity: u32,
    /// CA Descrambler control words
    pub cw: [u8; 8],
}

#[repr(C)]
#[derive(Default, Debug)]
pub struct CaPid {
    pub pid: u32,
    /// -1 == disable
    pub index: i32,
}

// pub const CA_GET_DESCR_INFO: IoctlInt = io_read::<CaDescrInfo>(b'o', 131);
// pub const CA_GET_MSG: IoctlInt = io_read::<CaMsg>(b'o', 132);
// pub const CA_SEND_MSG: IoctlInt = io_write::<CaMsg>(b'o', 133);
// pub const CA_SET_DESCR: IoctlInt = io_write::<CaDescr>(b'o', 134);
// pub const CA_SET_PID: IoctlInt = io_write::<CaPid>(b'o', 135);