# Copyright (c) 2009-2012, 2014-2017, The Linux Foundation. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of The Linux Foundation nor
#       the names of its contributors may be used to endorse or promote
#       products derived from this software without specific prior written
#       permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

import /vendor/etc/init/hw/init.qcom.usb.rc
import /vendor/etc/init/hw/init.target.rc
import /vendor/etc/init/hw/init.target.performance.rc
import /vendor/etc/init/init.parts.rc
import /vendor/etc/init/hw/init.sagit.rc

import /init.spectrum.rc

on early-init
    mount debugfs debugfs /sys/kernel/debug
    chmod 0755 /sys/kernel/debug

on post-fs
    chmod 0755 /sys/kernel/debug/tracing

    chmod 0664 /sys/devices/virtual/graphics/fb0/idle_time
    chown system graphics /sys/devices/virtual/graphics/fb0/idle_time
    write /sys/devices/virtual/graphics/fb0/idle_time 100

on post-fs-data
    mkdir /data/vendor/misc 01771 system system

    # Create directory used by display clients
    mkdir /data/vendor/display 0770 system graphics

    # Change lm related dirs
    mkdir /data/vendor/lm 0700 root root

    # Create directory used by media clients
    mkdir /data/vendor/media 0770 mediacodec media

    # Create directory for TZ Apps
    mkdir /data/misc/qsee 0770 system system

    # Create directory for apps access via QTEEConnector
    mkdir /data/vendor/qtee 0770 system system

    # Create folder for mm-qcamera-daemon
    mkdir /data/misc/camera 0770 camera camera
    setprop persist.camera.stats.test 5

    # Create directories for tombstones
    mkdir /data/tombstones/modem 0771 system system
    mkdir /data/tombstones/lpass 0771 system system
    mkdir /data/tombstones/wcnss 0771 system system
    mkdir /data/tombstones/dsps 0771 system system

    # Create directory for tftp
    mkdir /data/vendor/tombstones 0771 system system

    mkdir /data/vendor/ramdump 0771 root system
    mkdir /data/vendor/bluetooth 0770 bluetooth bluetooth
    mkdir /data/vendor/ramdump/bluetooth 0770 bluetooth bluetooth

    # Create the directories used by the Wireless subsystem
    mkdir /data/vendor/wifi 0770 wifi wifi
    mkdir /data/vendor/wifi/sockets 0770 wifi wifi
    mkdir /data/vendor/wifi/hostapd 0770 wifi wifi
    mkdir /data/vendor/wifi/hostapd/ctrl 0770 wifi wifi
    mkdir /data/vendor/wifi/wpa_supplicant 0770 wifi wifi
    mkdir /data/vendor/wifi/wpa 0770 wifi wifi
    mkdir /data/vendor/wifi/wpa/sockets 0770 wifi wifi

    # Create netmgr log dir
    mkdir /data/vendor/netmgr 0770 radio radio

    # Create ipacm log dir
    mkdir /data/vendor/ipa 0770 radio radio

    #Create QTI dir for logs
    mkdir /data/vendor/dataqti 0770 radio radio

    # Create the directories used by CnE subsystem
    mkdir /data/vendor/connectivity 0771 radio radio

    # Create directory used by audio subsystem
    mkdir /data/vendor/misc/audio 0770 audio audio
    mkdir /data/vendor/misc/audio/acdbdata 0770 media audio
    mkdir /data/vendor/misc/audio/acdbdata/delta 0770 media audio

    # Create directory for radio
    mkdir /data/vendor/radio 0770 system radio

    # Create directories for Location services
    mkdir /data/vendor/location 0770 gps gps
    mkdir /data/vendor/location/mq 0770 gps gps
    mkdir /data/vendor/location/xtwifi 0770 gps gps
    mkdir /dev/socket/location 0770 gps gps
    mkdir /dev/socket/location/mq 0770 gps gps
    mkdir /dev/socket/location/xtra 0770 gps gps

    # SSC/SLPI sensors
    mkdir /mnt/vendor/persist/sensors 0775 system root
    write /mnt/vendor/persist/sensors/sensors_settings 1
    chown system root /mnt/vendor/persist/sensors/sensors_settings
    chmod 0664 /mnt/vendor/persist/sensors/sensors_settings
    chown system system /mnt/vendor/persist/PRSensorData.txt
    chmod 0600 /mnt/vendor/persist/PRSensorData.txt

    setprop vold.post_fs_data_done 1

    # Create perfd related dirs
    mkdir /data/system/perfd 0770 root system
    chmod 2770 /data/system/perfd

    # Create /data/vendor/time folder for time-services
    mkdir /data/vendor/time/ 0700 system system

    # Mark the copy complete flag to not completed
    write /data/vendor/radio/copy_complete 0
    chown radio radio /data/vendor/radio/copy_complete
    chmod 0660 /data/vendor/radio/copy_complete

    # Setup permission for powerHAL
    chown system system /dev/stune/top-app/schedtune.boost
    chown system system /dev/cpu_dma_latency
    chown system system /sys/class/kgsl/kgsl-3d0/devfreq/min_freq
    chown system system /sys/class/kgsl/kgsl-3d0/devfreq/max_freq
    chown system system /sys/class/devfreq/soc:qcom,gpubw/min_freq
    chown system system /sys/devices/soc/soc:qcom,cpubw/devfreq/soc:qcom,cpubw/min_freq

on early-boot
    chown system graphics /sys/class/graphics/fb0/idle_time
    chown system graphics /sys/class/graphics/fb0/dyn_pu
    chown system graphics /sys/class/graphics/fb0/dynamic_fps
    chown system graphics /sys/class/graphics/fb0/modes
    chown system graphics /sys/class/graphics/fb0/mode
    chown system graphics /sys/class/graphics/fb0/msm_cmd_autorefresh_en
    chmod 0664 /sys/class/graphics/fb0/idle_time
    chmod 0664 /sys/class/graphics/fb0/dyn_pu
    chmod 0664 /sys/class/graphics/fb0/dynamic_fps
    chmod 0664 /sys/class/graphics/fb0/modes
    chmod 0664 /sys/class/graphics/fb0/mode
    chmod 0664 /sys/class/graphics/fb0/msm_cmd_autorefresh_en

    chown system graphics /sys/class/graphics/fb0/msm_fb_persist_mode
    chown system graphics /sys/class/graphics/fb0/lineptr_value
    chmod 0664 /sys/class/graphics/fb0/msm_fb_persist_mode
    chmod 0664 /sys/class/graphics/fb0/lineptr_value

    chown system graphics /sys/class/graphics/fb1/msm_fb_persist_mode
    chown system graphics /sys/class/graphics/fb1/lineptr_value
    chmod 0664 /sys/class/graphics/fb1/msm_fb_persist_mode
    chmod 0664 /sys/class/graphics/fb1/lineptr_value

    # SSR setup
    write /sys/bus/msm_subsys/devices/subsys0/restart_level "related"
    write /sys/bus/msm_subsys/devices/subsys1/restart_level "related"
    write /sys/bus/msm_subsys/devices/subsys2/restart_level "related"
    write /sys/bus/msm_subsys/devices/subsys3/restart_level "related"
    write /sys/bus/msm_subsys/devices/subsys4/restart_level "related"
    write /sys/bus/msm_subsys/devices/subsys5/restart_level "related"
    write /sys/bus/msm_subsys/devices/subsys6/restart_level "related"

on boot
    chown bluetooth net_bt /sys/class/rfkill/rfkill0/device/extldo
    chown bluetooth net_bt /sys/class/rfkill/rfkill0/state
    chown bluetooth net_bt /sys/class/rfkill/rfkill0/type
    chmod 0660 /sys/class/rfkill/rfkill0/device/extldo
    chmod 0660 /sys/class/rfkill/rfkill0/state

    chown system system /sys/module/msm_core/parameters/polling_interval
    chown system system /sys/module/msm_core/parameters/disabled
    chown system system /sys/kernel/debug/msm_core/enable
    chown system system /sys/kernel/debug/msm_core/ptable

    # Create QMUX deamon socket area
    mkdir /dev/socket/qmux_radio 2770 radio radio
    mkdir /dev/socket/qmux_audio 2770 media audio
    mkdir /dev/socket/qmux_bluetooth 2770 bluetooth bluetooth
    mkdir /dev/socket/qmux_gps 2770 gps gps

    # Create NETMGR daemon socket area
    mkdir /dev/socket/netmgr 0750 radio radio

    # KeyDisabler permissions
    chown system system /sys/devices/soc/soc:fingerprint_fpc/enable_key_events
    chown system system /sys/devices/soc/soc:fingerprint_goodix/enable_key_events
    chmod 0660 /sys/devices/soc/soc:fingerprint_fpc/enable_key_events
    chmod 0660 /sys/devices/soc/soc:fingerprint_goodix/enable_key_events

    chown system system /proc/touchpanel/capacitive_keys_enable
    chown system system /proc/touchpanel/reversed_keys_enable
    chown system system /proc/touchpanel/wake_gesture
    chmod 0660 /proc/touchpanel/capacitive_keys_enable
    chmod 0660 /proc/touchpanel/reversed_keys_enable
    chmod 0660 /proc/touchpanel/wake_gesture

    # sweep2sleep permissions
    chown system system /sys/sweep2sleep/vib_strength
    chown system system /sys/sweep2sleep/sweep2sleep

    # USB Fastcharge
    chown system system /sys/kernel/fast_charge/force_fast_charge
    chmod 0660 /sys/kernel/fast_charge/force_fast_charge

    # KCAL permissions
    chown system system /sys/devices/platform/kcal_ctrl.0/kcal
    chown system system /sys/devices/platform/kcal_ctrl.0/kcal_enable
    chown system system /sys/devices/platform/kcal_ctrl.0/kcal_cont
    chown system system /sys/devices/platform/kcal_ctrl.0/kcal_sat
    chown system system /sys/devices/platform/kcal_ctrl.0/kcal_hue
    chown system system /sys/devices/platform/kcal_ctrl.0/kcal_min
    chown system system /sys/devices/platform/kcal_ctrl.0/kcal_val
    chmod 0660 /sys/devices/platform/kcal_ctrl.0/kcal
    chmod 0660 /sys/devices/platform/kcal_ctrl.0/kcal_enable
    chmod 0660 /sys/devices/platform/kcal_ctrl.0/kcal_cont
    chmod 0660 /sys/devices/platform/kcal_ctrl.0/kcal_sat
    chmod 0660 /sys/devices/platform/kcal_ctrl.0/kcal_hue
    chmod 0660 /sys/devices/platform/kcal_ctrl.0/kcal_min
    chmod 0660 /sys/devices/platform/kcal_ctrl.0/kcal_val

    # DT2W
    chown system system /dev/input/event1
    chmod 0666 /dev/input/event1

    # Assign TCP buffer thresholds to be ceiling value of technology maximums
    # Increased technology maximums should be reflected here.
    write /proc/sys/net/core/rmem_max 8388608
    write /proc/sys/net/core/wmem_max 8388608

    # To prevent out of order acknowledgements from making
    # connection tracking to treat them as not belonging to
    # the connection they belong to.
    # Otherwise, a weird issue happens in which some long
    # connections on high-throughput links get dropped when
    # an ack packet comes out of order
    write /proc/sys/net/netfilter/nf_conntrack_tcp_be_liberal 1

    # Wifi firmware reload path
    chown wifi wifi /sys/module/wlan/parameters/fwpath

on property:sys.boot_completed=1
    # Used by sound trigger HAL
    chown media audio /sys/kernel/wdsp0/boot
    chown media audio /sys/kernel/wcd_cpe0/fw_name

service vendor.cnd /vendor/bin/cnd
    class main
    user system
    group system wifi inet radio wakelock net_admin
    writepid /dev/cpuset/system-background/tasks

service vendor.dpmQmiMgr /vendor/bin/dpmQmiMgr
    class main
    user system
    group system

service vendor.irsc_util /vendor/bin/irsc_util "/vendor/etc/sec_config"
    class core
    user root
    oneshot
    writepid /dev/cpuset/system-background/tasks

service vendor.rmt_storage /vendor/bin/rmt_storage
    class core
    user root
    shutdown critical
    ioprio rt 0

service vendor.tftp_server /vendor/bin/tftp_server
    class core
    user root
    group root system

on property:sys.post_boot.parsed=1
    # Setup permission for powerHAL
    chown system system /sys/devices/soc/soc:qcom,cpubw/devfreq/soc:qcom,cpubw/bw_hwmon/hyst_trigger_count
    chown system system /sys/devices/soc/soc:qcom,cpubw/devfreq/soc:qcom,cpubw/bw_hwmon/hist_memory
    chown system system /sys/devices/soc/soc:qcom,cpubw/devfreq/soc:qcom,cpubw/bw_hwmon/hyst_length

service vendor.atfwd /vendor/bin/ATFWD-daemon
    class late_start
    user system
    group system radio
    writepid /dev/cpuset/system-background/tasks

service vendor.netmgrd /vendor/bin/netmgrd
    class main
    writepid /dev/cpuset/system-background/tasks

service vendor.ipacm /vendor/bin/ipacm
    class main
    user radio
    group radio inet
    writepid /dev/cpuset/system-background/tasks

service vendor.dataqti /vendor/bin/qti
    class main
    user radio
    group radio oem_2901 diag usb net_admin
    writepid /dev/cpuset/system-background/tasks

service vendor.sensors /vendor/bin/sensors.qti
    class main
    user root
    group root
    writepid /dev/cpuset/system-background/tasks

service wpa_supplicant /vendor/bin/hw/wpa_supplicant \
    -O/data/vendor/wifi/wpa/sockets -puse_p2p_group_interface=1 \
    -g@android:wpa_wlan0
    interface android.hardware.wifi.supplicant@1.0::ISupplicant default
    interface android.hardware.wifi.supplicant@1.1::ISupplicant default
    class main
    socket wpa_wlan0 dgram 660 wifi wifi
    disabled
    oneshot
    writepid /dev/cpuset/system-background/tasks

service vendor.cnss-daemon /vendor/bin/cnss-daemon -n -l
    class late_start
    user system
    group system inet net_admin wifi
    capabilities NET_ADMIN
    writepid /dev/cpuset/system-background/tasks

on property:sys.shutdown.requested=*
    stop vendor.cnss-daemon

service vendor.loc_launcher /vendor/bin/loc_launcher
    class late_start
    user gps
    group gps
    writepid /dev/cpuset/system-background/tasks

service vendor.qcom-sh /vendor/bin/init.qcom.sh
    class late_start
    user root
    oneshot

service vendor.qcom-post-boot /vendor/bin/init.qcom.post_boot.sh
    class late_start
    user root
    disabled
    oneshot

service vendor.panel-info /vendor/bin/init.panel_info.sh
    class core
    user root
    oneshot

service vendor.ril-daemon2 /vendor/bin/hw/rild -c 2
    class main
    socket rild2 stream 660 root radio
    socket rild-debug2 stream 660 radio system
    user radio
    group radio cache inet misc audio sdcard_r sdcard_rw diag log
    capabilities BLOCK_SUSPEND NET_ADMIN NET_RAW

service charger /charger
    class charger
    group system graphics
    seclabel u:r:healthd:s0

service vendor.msm_irqbalance /vendor/bin/msm_irqbalance -f /vendor/etc/msm_irqbalance.conf
    class core
    user root
    group root

service vendor.time_daemon /vendor/bin/time_daemon
    disabled
    class main
    user root
    group root
    writepid /dev/cpuset/system-background/tasks
