summarylogtreecommitdiffstats
path: root/sc0710.install
blob: 2002c95eb9bd17a5c056b539a92fd0a72aa71c0a (plain)
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
#!/bin/bash

# 4K Pro detection and card naming use /usr/lib/sc0710/sc0710-firmware-lib.sh

DRV_NAME="sc0710"
FW_LIB="/usr/lib/sc0710/sc0710-firmware-lib.sh"
EXTRACT_SCRIPT="/usr/lib/sc0710/extract-firmware.sh"
FIRMWARE_FILE="SC0710.FWI.HEX"

is_4k_pro() {
    if [[ -f "$FW_LIB" ]]; then
        # shellcheck source=/dev/null
        source "$FW_LIB"
        sc0710_is_4k_pro
        return $?
    fi
    lspci -n -v -d 12ab:0710 2>/dev/null | grep -qi "1cfa:0012"
}

detect_card_name() {
    if [[ -f "$FW_LIB" ]]; then
        # shellcheck source=/dev/null
        source "$FW_LIB"
        sc0710_detect_card_name
        return $?
    fi
    return 1
}

print_mk2_next_steps() {
    echo "To load the module now:"
    echo "  sudo modprobe sc0710"
    echo ""
    echo "Optional boot load:"
    echo "  echo sc0710 | sudo tee /etc/modules-load.d/sc0710.conf"
    echo ""
    echo "Then check status:"
    echo "  sc0710-cli --status"
}

print_4k_pro_next_steps() {
    echo "4K Pro — the driver programs the ECP5 FPGA when it loads, and refuses"
    echo "to bind the card if it can't (missing firmware file, upload failure)."
    echo ""
    echo "Check driver and firmware status:"
    echo "  sc0710-cli --status"
    echo ""
    echo "If the card didn't bind (no /dev/video*):"
    echo "  sudo dmesg | grep sc0710       (shows why the probe failed)"
    echo "  sudo bash /usr/lib/sc0710/extract-firmware.sh   (if firmware is missing)"
    echo "  sudo sc0710-cli --restart"
}

firmware_present() {
    [[ -f "/lib/firmware/sc0710/$FIRMWARE_FILE" ]] || \
    [[ -f "/etc/firmware/sc0710/$FIRMWARE_FILE" ]] || \
    [[ -f "/var/lib/sc0710/firmware/$FIRMWARE_FILE" ]]
}

setup_modprobe_boot() {
    if [[ ! -f "/etc/modules-load.d/${DRV_NAME}.conf" ]]; then
        echo "$DRV_NAME" > "/etc/modules-load.d/${DRV_NAME}.conf"
    fi
    if [[ ! -f "/etc/modprobe.d/${DRV_NAME}.conf" ]]; then
        cat > "/etc/modprobe.d/${DRV_NAME}.conf" <<EOF
softdep ${DRV_NAME} pre: videodev videobuf2-v4l2 videobuf2-vmalloc videobuf2-common snd-pcm
EOF
    fi
}

setup_4k_pro() {
    echo "Configuring ECP5 firmware for 4K Pro..."

    if ! firmware_present && [[ -x "$EXTRACT_SCRIPT" ]]; then
        echo "Extracting ECP5 firmware (requires network)..."
        if ! bash "$EXTRACT_SCRIPT"; then
            echo "warning: firmware extraction failed; the driver will refuse to bind the"
            echo "card until the firmware is installed: sudo bash $EXTRACT_SCRIPT"
        fi
    fi

    setup_modprobe_boot
}

# Older package versions created boot-time firmware units outside the package;
# the driver programs the ECP5 itself now, so scrub the stale files on upgrade.
remove_legacy_firmware_units() {
    local u
    for u in sc0710-firmware.service sc0710-firmware-verify.service; do
        systemctl disable --now "$u" 2>/dev/null || true
        rm -f "/etc/systemd/system/$u"
    done
    systemctl daemon-reload 2>/dev/null || true
}

_dkms_cleanup() {
    local ver_item

    lsmod | grep -q "^${DRV_NAME} " && rmmod "$DRV_NAME" 2>/dev/null || true

    for ver_item in $(dkms status 2>/dev/null | awk -F'[:,]' "/^${DRV_NAME}/ {print \$1}" | tr -d ' '); do
        dkms remove "$ver_item" --all >/dev/null 2>&1 || \
            rm -rf "/var/lib/dkms/$(echo "$ver_item" | tr ',' '/')" 2>/dev/null
    done
    rmdir "/var/lib/dkms/${DRV_NAME}" 2>/dev/null || true
    rm -rf /usr/src/${DRV_NAME}-*

    # Drop stale modules left when an older DKMS version was removed without --all.
    find /usr/lib/modules -path "*/updates/dkms/${DRV_NAME}.ko*" -delete 2>/dev/null || true
    find /usr/lib/modules -path "*/kernel/drivers/media/pci/${DRV_NAME}.ko*" -delete 2>/dev/null || true
    depmod -a 2>/dev/null || true
}

pre_install() {
    _dkms_cleanup
}

pre_upgrade() {
    _dkms_cleanup
}

post_install_common() {
    local card_name=""

    echo ""
    echo "Installed: DKMS module, sc0710-cli, and /usr/lib/sc0710/ firmware helpers."
    echo ""

    if card_name=$(detect_card_name); then
        echo "Detected card: ${card_name}"
    else
        echo "Detected card: none (no 12ab:0710 device found)"
    fi
    echo ""
    echo "Manage the driver with:"
    echo "  sc0710-cli --status"
    echo "  sc0710-cli --help"
    echo ""

    if is_4k_pro; then
        setup_4k_pro
        echo ""
        print_4k_pro_next_steps
    else
        print_mk2_next_steps
    fi
}

post_install() {
    post_install_common
    _dkms_finalize
}

post_upgrade() {
    remove_legacy_firmware_units
    post_install_common
    _dkms_finalize
}

_dkms_finalize() {
    local lib="/usr/lib/sc0710/sc0710-dkms-lib.sh"

    if [[ -f "$lib" ]]; then
        # shellcheck source=/dev/null
        source "$lib"
        sc0710_dkms_ensure_installed
    fi

    for dep in videodev videobuf2-common videobuf2-v4l2 videobuf2-vmalloc snd-pcm; do
        modprobe "$dep" 2>/dev/null || true
    done
    modprobe "$DRV_NAME" 2>/dev/null || true
}

pre_remove() {
    _dkms_cleanup
}