#
# Copyright (C) 2024 The LineageOS Project
#
# SPDX-License-Identifier: Apache-2.0
#

import /vendor/etc/init/hw/init.mi_thermald.rc
import /vendor/etc/init/hw/init.batterysecret.rc

on fs
    mkdir /mnt/vendor/persist/haptics 0755 system system
    mkdir /mnt/vendor/persist/audio 0755 system system
    mkdir /mnt/vendor/persist/qca6490/ 0700 system system

on boot
    # modem
    chmod 0777 /dev/smd8
    chown radio radio /dev/smd8
    mkdir /data/vendor/modem 0777 radio radio

    chown system system /sys/class/thermal/thermal_message/sconfig
    chown system system /sys/class/thermal/thermal_message/charger_temp
    chown system system /sys/class/thermal/thermal_message/board_sensor_temp_comp
    chown system system /sys/class/thermal/thermal_message/cpu_nolimit_temp
    chown system system /sys/class/thermal/thermal_message/balance_mode
    chown cameraserver cameraserver /sys/class/thermal/thermal_message/flash_state
    chown system system /sys/module/perf_helper/mimd/mimdtrigger

    # Create persist for camera calibration data
    mkdir /mnt/vendor/persist/camera 0755 system cameraserver

    # Set fingerprint related permissions
    chmod 0660 /dev/goodix_fp
    chown system system /dev/goodix_fp
    chown system system /sys/bus/platform/devices/soc:fingerprint_fpc/irq
    chown system system /sys/bus/platform/devices/soc:fingerprint_fpc/irq_enable
    chown system system /sys/bus/platform/devices/soc:fingerprint_fpc/wakeup_enable
    chown system system /sys/bus/platform/devices/soc:fingerprint_fpc/hw_reset
    chown system system /sys/bus/platform/devices/soc:fingerprint_fpc/device_prepare
    chown system system /sys/bus/platform/devices/soc:fingerprint_fpc/fingerdown_wait
    chown system system /sys/bus/platform/devices/soc:fingerprint_fpc/vendor
    chown system system /sys/bus/platform/devices/soc:fingerprint_fpc/request_vreg
    chown system system /sys/bus/platform/devices/soc:fingerprint_fpc/simulate_irq
    chown system system /sys/bus/platform/devices/soc:fingerprint_fpc/finger_irq
    chown system system /sys/bus/platform/devices/soc:fingerprint_fpc/power_cfg

    # Xiaomi touch
    chown system system /dev/xiaomi-touch
    chmod 0660 /dev/xiaomi-touch

    # Touchscreen sensors
    chown system system /sys/class/touch/touch_dev/fod_longpress_gesture_enabled
    chown system system /sys/class/touch/touch_dev/fod_press_status
    chmod 0660 /sys/class/touch/touch_dev/fod_longpress_gesture_enabled
    chmod 0660 /sys/class/touch/touch_dev/fod_press_status

    chown system system /sys/class/touch/touch_dev/gesture_single_tap_enabled
    chown system system /sys/class/touch/touch_dev/gesture_single_tap_state
    chmod 0660 /sys/class/touch/touch_dev/gesture_single_tap_enabled
    chmod 0660 /sys/class/touch/touch_dev/gesture_single_tap_state

    chown system system /sys/class/touch/touch_dev/gesture_double_tap_enabled
    chown system system /sys/class/touch/touch_dev/gesture_double_tap_state
    chmod 0660 /sys/class/touch/touch_dev/gesture_double_tap_enabled
    chmod 0660 /sys/class/touch/touch_dev/gesture_double_tap_state

on post-fs-data
    # Create folder of camera
    mkdir /data/vendor/camera/offlinelog 0777 cameraserver audio
    chmod 0666 /dev/camlog

    # Create the directories used by the Wireless subsystem
    mkdir /data/vendor/mac_addr 0770 system wifi

    mkdir /data/vendor/thermal 0771 root system
    mkdir /data/vendor/thermal/config 0771 root system
    mkdir /data/vendor/perfspy 0775 camera camera
    chmod 0660 /dev/ir_spi
    chown system system /dev/ir_spi

    # Display feature sysfs node
    chown system system /sys/class/mi_display/disp-DSI-0/disp_param
    chmod 0664 /sys/class/mi_display/disp-DSI-0/disp_param
    chown system system /sys/class/mi_display/disp-DSI-0/mipi_rw
    chmod 0664 /sys/class/mi_display/disp-DSI-0/mipi_rw
    chown system system /sys/class/mi_display/disp-DSI-0/panel_info
    chmod 0444 /sys/class/mi_display/disp-DSI-0/panel_info
    chown system system /sys/class/mi_display/disp-DSI-0/wp_info
    chmod 0444 /sys/class/mi_display/disp-DSI-0/wp_info
    chown system system /sys/class/mi_display/disp-DSI-0/dynamic_fps
    chmod 0444 /sys/class/mi_display/disp-DSI-0/dynamic_fps
    chown system system /sys/class/mi_display/disp-DSI-0/doze_brightness
    chmod 0664 /sys/class/mi_display/disp-DSI-0/doze_brightness
    chown system system /sys/class/mi_display/disp-DSI-0/brightness_clone
    chmod 0664 /sys/class/mi_display/disp-DSI-0/brightness_clone
    chown system system /sys/class/mi_display/disp-DSI-0/max_brightness_clone
    chmod 0664 /sys/class/mi_display/disp-DSI-0/max_brightness_clone

    # Display feature sysfs node for penel1
    chown system system /sys/class/mi_display/disp-DSI-1/brightness_clone
    chmod 0664 /sys/class/mi_display/disp-DSI-1/brightness_clone
    chown system system /sys/class/mi_display/disp-DSI-1/max_brightness_clone
    chmod 0664 /sys/class/mi_display/disp-DSI-1/max_brightness_clone

    # Create fingerprint related directories
    mkdir /data/vendor/fpc 0770 system system
    mkdir /data/vendor/fpdump 0770 system system
    mkdir /data/vendor/goodix 0770 system system
    mkdir /mnt/vendor/persist/fpc 0770 system system
    mkdir /mnt/vendor/persist/goodix 0770 system system

on property:sys.boot_completed=1
    # Set thermal mode to mgame
    write /sys/devices/virtual/thermal/thermal_message/sconfig 20

on property:persist.sys.xtra-daemon.enabled=*
    restart loc_launcher

service vendor.nv_mac /vendor/bin/nv_mac
    class late_start
    user system
    group system inet net_admin wifi net_raw
    oneshot

service vendor.audio-hal /vendor/bin/hw/android.hardware.audio.service
    override
    class hal
    user audioserver
    # media gid needed for /dev/fm (radio) and for /data/misc/media (tee)
    group audio camera drmrpc inet media mediadrm net_bt net_bt_admin net_bw_acct oem_2901 wakelock context_hub
    capabilities BLOCK_SUSPEND SYS_NICE
    # setting RLIMIT_RTPRIO allows binder RT priority inheritance
    rlimit rtprio 10 10
    ioprio rt 4
    task_profiles ProcessCapacityHigh HighPerformance
    onrestart restart audioserver
    socket audio_us_socket_0 seqpacket 0666 system system

service vendor.sensors-hal-multihal /vendor/bin/hw/android.hardware.sensors-service.xiaomi-multihal
    override
    class hal
    user system
    group system wakelock context_hub input uhid
    task_profiles ServiceCapacityLow
    capabilities BLOCK_SUSPEND
    rlimit rtprio 10 10
    socket audio_us_socket_1 seqpacket 0666 system system

on property:ro.boot.hwversion=*
    setprop ro.boot.hardware.revision ${ro.boot.hwc}_${ro.boot.hwlevel}_${ro.boot.hwversion}
