on early-boot
    mkdir /config/usb_gadget/g1
    mkdir /config/usb_gadget/g1/strings/0x409
    mkdir /config/usb_gadget/g1/configs/b.1
    write /config/usb_gadget/g1/idVendor 0x18d1
    write /config/usb_gadget/g1/strings/0x409/serialnumber ${ro.serialno}
    write /config/usb_gadget/g1/strings/0x409/manufacturer ${ro.product.manufacturer}
    write /config/usb_gadget/g1/strings/0x409/product ${ro.product.model}
    write /config/usb_gadget/g1/configs/b.1/MaxPower 0x384
    write /config/usb_gadget/g1/os_desc/b_vendor_code 0x1
    write /config/usb_gadget/g1/os_desc/qw_sign "MSFT100"

    # ffs function
    mkdir /config/usb_gadget/g1/functions/ffs.adb 0770 shell shell
    mkdir /config/usb_gadget/g1/functions/ffs.mtp
    mkdir /config/usb_gadget/g1/functions/ffs.ptp
    mkdir /dev/usb-ffs 0775 shell shell
    mkdir /dev/usb-ffs/adb 0770 shell shell
    mkdir /dev/usb-ffs/mtp 0770 mtp mtp
    mkdir /dev/usb-ffs/ptp 0770 mtp mtp
    mount functionfs adb /dev/usb-ffs/adb rmode=0770,fmode=0660,uid=2000,gid=2000,no_disconnect=1
    mount functionfs mtp /dev/usb-ffs/mtp rmode=0770,fmode=0660,uid=1024,gid=1024,no_disconnect=1
    mount functionfs ptp /dev/usb-ffs/ptp rmode=0770,fmode=0660,uid=1024,gid=1024,no_disconnect=1
    setprop sys.usb.mtp.device_type 3
    setprop sys.usb.mtp.batchcancel true
    symlink /config/usb_gadget/g1/configs/b.1 /config/usb_gadget/g1/os_desc/b.1

    # mtp function
    mkdir /config/usb_gadget/g1/functions/mtp.gs0

    # ptp function
    mkdir /config/usb_gadget/g1/functions/ptp.gs1

    # accessory function
    mkdir /config/usb_gadget/g1/functions/accessory.gs2

    # audio_source function
    mkdir /config/usb_gadget/g1/functions/audio_source.gs3

    # rndis function
    mkdir /config/usb_gadget/g1/functions/rndis.gs4
    # Modify class/subclass/protocol for rndis.gs4
    # Remote NDIS: Class: Wireless Controller (0xe0), Subclass: 0x1, Protocol: 0x3
    write /config/usb_gadget/g1/functions/rndis.gs4/class e0
    write /config/usb_gadget/g1/functions/rndis.gs4/subclass 01
    write /config/usb_gadget/g1/functions/rndis.gs4/protocol 03
    write /config/usb_gadget/g1/functions/rndis.gs4/os_desc/interface.ncm/compatible_id RNDIS
    write /config/usb_gadget/g1/functions/rndis.gs4/ifname rndis%d
    setprop vendor.usb.rndis.config rndis.gs4

    # midi function
    mkdir /config/usb_gadget/g1/functions/midi.gs5

    # acm function
    mkdir /config/usb_gadget/g1/functions/acm.gs6

    # dm function
    mkdir /config/usb_gadget/g1/functions/dm.gs7

    # uts function
    mkdir /config/usb_gadget/g1/functions/uts.gs8

    # ncm function
    mkdir /config/usb_gadget/g1/functions/ncm.gs9
    write /config/usb_gadget/g1/functions/ncm.gs9/os_desc/interface.ncm/compatible_id WINNCM
    write /config/usb_gadget/g1/functions/ncm.gs9/ifname ncm%d

    # etr_miu function
    mkdir /config/usb_gadget/g1/functions/etr_miu.gs11

    # acm function for uwb
    mkdir /config/usb_gadget/g1/functions/acm.uwb0

    # uvc function
    mkdir /config/usb_gadget/g1/functions/uvc.0
    write /config/usb_gadget/g1/functions/uvc.0/function_name "Android Webcam"
    write /config/usb_gadget/g1/functions/uvc.0/streaming_maxpacket 3072
    # write /config/usb_gadget/g1/functions/uvc.0/streaming_maxburst 5
    # write /config/usb_gadget/g1/functions/uvc.0/streaming_interval 2
    # setup control params
    mkdir /config/usb_gadget/g1/functions/uvc.0/control/header/h
    symlink /config/usb_gadget/g1/functions/uvc.0/control/header/h /config/usb_gadget/g1/functions/uvc.0/control/class/fs/h
    symlink /config/usb_gadget/g1/functions/uvc.0/control/header/h /config/usb_gadget/g1/functions/uvc.0/control/class/ss/h
    mkdir /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m
    # advertise 360p resolution for webcam encoded as mjpeg
    mkdir /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/360p
    write /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/360p/wHeight 360
    write /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/360p/wWidth 640
    write /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/360p/dwMaxVideoFrameBufferSize 460800
    write /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/360p/dwDefaultFrameInterval 333333
    write /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/360p/dwFrameInterval "166666
333333
416666
666666"
    # advertise 480p resolution for webcam encoded as mjpeg
    mkdir /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/480p
    write /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/480p/wHeight 480
    write /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/480p/wWidth 640
    write /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/480p/dwMaxVideoFrameBufferSize 614400
    write /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/480p/dwDefaultFrameInterval 333333
    write /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/480p/dwFrameInterval "166666
333333
416666
666666"
    # advertise 720p resolution for webcam encoded as mjpeg
    mkdir /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/720p
    write /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/720p/wHeight 720
    write /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/720p/wWidth 1280
    write /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/720p/dwMaxVideoFrameBufferSize 1843200
    write /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/720p/dwDefaultFrameInterval 333333
    write /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/720p/dwFrameInterval "166666
333333
416666
666666"
    # advertise 1080p resolution for webcam encoded as mjpeg
    mkdir /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/1080p
    write /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/1080p/wHeight 1080
    write /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/1080p/wWidth 1920
    write /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/1080p/dwMaxVideoFrameBufferSize 4147200
    write /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/1080p/dwDefaultFrameInterval 333333
    write /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/1080p/dwFrameInterval "166666
333333
416666
666666"
    # TODO: Add uncompressed streams
    # setup streaming params
    mkdir /config/usb_gadget/g1/functions/uvc.0/streaming/header/h
    symlink /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m /config/usb_gadget/g1/functions/uvc.0/streaming/header/h/m
    symlink /config/usb_gadget/g1/functions/uvc.0/streaming/header/h /config/usb_gadget/g1/functions/uvc.0/streaming/class/fs/h
    symlink /config/usb_gadget/g1/functions/uvc.0/streaming/header/h /config/usb_gadget/g1/functions/uvc.0/streaming/class/hs/h
    symlink /config/usb_gadget/g1/functions/uvc.0/streaming/header/h /config/usb_gadget/g1/functions/uvc.0/streaming/class/ss/h
    # remove write permissions for 'others'
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/control/header/h/bcdUVC
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/control/header/h/dwClockFrequency
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/control/terminal/output/default/bSourceID
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/function_name
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming_interval
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming_maxburst
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming_maxpacket
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/1080p/bmCapabilities
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/1080p/dwDefaultFrameInterval
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/1080p/dwFrameInterval
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/1080p/dwMaxBitRate
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/1080p/dwMaxVideoFrameBufferSize
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/1080p/dwMinBitRate
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/1080p/wHeight
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/1080p/wWidth
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/360p/bmCapabilities
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/360p/dwDefaultFrameInterval
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/360p/dwFrameInterval
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/360p/dwMaxBitRate
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/360p/dwMaxVideoFrameBufferSize
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/360p/dwMinBitRate
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/360p/wHeight
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/360p/wWidth
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/480p/bmCapabilities
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/480p/dwDefaultFrameInterval
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/480p/dwFrameInterval
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/480p/dwMaxBitRate
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/480p/dwMaxVideoFrameBufferSize
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/480p/dwMinBitRate
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/480p/wHeight
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/480p/wWidth
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/720p/bmCapabilities
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/720p/dwDefaultFrameInterval
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/720p/dwFrameInterval
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/720p/dwMaxBitRate
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/720p/dwMaxVideoFrameBufferSize
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/720p/dwMinBitRate
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/720p/wHeight
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/720p/wWidth
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/bDefaultFrameIndex
    chmod 664 /config/usb_gadget/g1/functions/uvc.0/streaming/mjpeg/m/bmaControls

    # chown file/folder permission
    chown system system /config/usb_gadget/
    chown system system /config/usb_gadget/g1
    chown system system /config/usb_gadget/g1/UDC
    chown system system /config/usb_gadget/g1/bDeviceClass
    chown system system /config/usb_gadget/g1/bDeviceProtocol
    chown system system /config/usb_gadget/g1/bDeviceSubClass
    chown system system /config/usb_gadget/g1/bMaxPacketSize0
    chown system system /config/usb_gadget/g1/bcdDevice
    chown system system /config/usb_gadget/g1/bcdUSB
    chown system system /config/usb_gadget/g1/configs
    chown system system /config/usb_gadget/g1/configs/b.1
    chown system system /config/usb_gadget/g1/configs/b.1/MaxPower
    chown system system /config/usb_gadget/g1/configs/b.1/bmAttributes
    chown system system /config/usb_gadget/g1/configs/b.1/strings
    chown system system /config/usb_gadget/g1/functions
    chown system system /config/usb_gadget/g1/functions/accessory.gs2
    chown system system /config/usb_gadget/g1/functions/acm.gs6
    chown system system /config/usb_gadget/g1/functions/acm.gs6/port_num
    chown system system /config/usb_gadget/g1/functions/acm.uwb0
    chown system system /config/usb_gadget/g1/functions/acm.uwb0/port_num
    chown system system /config/usb_gadget/g1/functions/audio_source.gs3
    chown system system /config/usb_gadget/g1/functions/dm.gs7
    chown system system /config/usb_gadget/g1/functions/ffs.adb
    chown system system /config/usb_gadget/g1/functions/ffs.mtp
    chown system system /config/usb_gadget/g1/functions/ffs.ptp
    chown system system /config/usb_gadget/g1/functions/midi.gs5
    chown system system /config/usb_gadget/g1/functions/midi.gs5/buflen
    chown system system /config/usb_gadget/g1/functions/midi.gs5/id
    chown system system /config/usb_gadget/g1/functions/midi.gs5/in_ports
    chown system system /config/usb_gadget/g1/functions/midi.gs5/index
    chown system system /config/usb_gadget/g1/functions/midi.gs5/out_ports
    chown system system /config/usb_gadget/g1/functions/midi.gs5/qlen
    chown system system /config/usb_gadget/g1/functions/mtp.gs0
    chown system system /config/usb_gadget/g1/functions/mtp.gs0/os_desc
    chown system system /config/usb_gadget/g1/functions/mtp.gs0/os_desc/interface.MTP
    chown system system /config/usb_gadget/g1/functions/mtp.gs0/os_desc/interface.MTP/compatible_id
    chown system system /config/usb_gadget/g1/functions/mtp.gs0/os_desc/interface.MTP/sub_compatible_id
    chown system system /config/usb_gadget/g1/functions/ncm.gs9
    chown system system /config/usb_gadget/g1/functions/ncm.gs9/dev_addr
    chown system system /config/usb_gadget/g1/functions/ncm.gs9/host_addr
    chown system system /config/usb_gadget/g1/functions/ncm.gs9/ifname
    chown system system /config/usb_gadget/g1/functions/ncm.gs9/os_desc
    chown system system /config/usb_gadget/g1/functions/ncm.gs9/os_desc/interface.ncm
    chown system system /config/usb_gadget/g1/functions/ncm.gs9/os_desc/interface.ncm/compatible_id
    chown system system /config/usb_gadget/g1/functions/ncm.gs9/os_desc/interface.ncm/sub_compatible_id
    chown system system /config/usb_gadget/g1/functions/ncm.gs9/qmult
    chown system system /config/usb_gadget/g1/functions/ptp.gs1
    chown system system /config/usb_gadget/g1/functions/ptp.gs1/os_desc
    chown system system /config/usb_gadget/g1/functions/ptp.gs1/os_desc/interface.MTP
    chown system system /config/usb_gadget/g1/functions/ptp.gs1/os_desc/interface.MTP/compatible_id
    chown system system /config/usb_gadget/g1/functions/ptp.gs1/os_desc/interface.MTP/sub_compatible_id
    chown system system /config/usb_gadget/g1/functions/rndis.gs4
    chown system system /config/usb_gadget/g1/functions/rndis.gs4/class
    chown system system /config/usb_gadget/g1/functions/rndis.gs4/dev_addr
    chown system system /config/usb_gadget/g1/functions/rndis.gs4/host_addr
    chown system system /config/usb_gadget/g1/functions/rndis.gs4/ifname
    chown system system /config/usb_gadget/g1/functions/rndis.gs4/os_desc
    chown system system /config/usb_gadget/g1/functions/rndis.gs4/os_desc/interface.rndis
    chown system system /config/usb_gadget/g1/functions/rndis.gs4/os_desc/interface.rndis/compatible_id
    chown system system /config/usb_gadget/g1/functions/rndis.gs4/os_desc/interface.rndis/sub_compatible_id
    chown system system /config/usb_gadget/g1/functions/rndis.gs4/protocol
    chown system system /config/usb_gadget/g1/functions/rndis.gs4/qmult
    chown system system /config/usb_gadget/g1/functions/rndis.gs4/subclass
    chown system system /config/usb_gadget/g1/idProduct
    chown system system /config/usb_gadget/g1/idVendor
    chown system system /config/usb_gadget/g1/max_speed
    chown system system /config/usb_gadget/g1/os_desc
    chown system system /config/usb_gadget/g1/os_desc/b.1
    chown system system /config/usb_gadget/g1/os_desc/b_vendor_code
    chown system system /config/usb_gadget/g1/os_desc/qw_sign
    chown system system /config/usb_gadget/g1/os_desc/use
    chown system system /config/usb_gadget/g1/strings
    chown system system /config/usb_gadget/g1/strings/0x409
    chown system system /config/usb_gadget/g1/strings/0x409/manufacturer
    chown system system /config/usb_gadget/g1/strings/0x409/product
    chown system system /config/usb_gadget/g1/strings/0x409/serialnumber
    setprop vendor.usb.functions.ready 1

on boot
    # Set USB timeout
    write sys/module/usbcore/parameters/initial_descriptor_timeout 500
    # Use USB Gadget HAL
    setprop sys.usb.configfs 2
    # Enable in-kernel media-presence polling for SD cards
    write /sys/module/block/parameters/events_dfl_poll_msecs 2000

on property:sys.usb.ffs.ready=1 && property:sys.usb.config=adb && property:sys.usb.configfs=1
    write /config/usb_gadget/g1/idProduct 0x4EE7
    write /config/usb_gadget/g1/idVendor 0x18D1

on property:vendor.usb.config=*
   start usbd

on property:persist.vendor.usb.usbradio.config=*
   start usbd

on charger
    mkdir /config/usb_gadget/g1
    mkdir /config/usb_gadget/g1/strings/0x409
    mkdir /config/usb_gadget/g1/configs/b.1
    write /config/usb_gadget/g1/strings/0x409/serialnumber ${ro.serialno}
    write /config/usb_gadget/g1/strings/0x409/manufacturer ${ro.product.manufacturer}
    write /config/usb_gadget/g1/strings/0x409/product ${ro.product.model}
    write /config/usb_gadget/g1/configs/b.1/MaxPower 0x384
    write /config/usb_gadget/g1/os_desc/b_vendor_code 0x1
    write /config/usb_gadget/g1/os_desc/qw_sign "MSFT100"
    mkdir /config/usb_gadget/g1/functions/midi.gs5
    symlink /config/usb_gadget/g1/configs/b.1 /config/usb_gadget/g1/os_desc/b.1
    setprop sys.usb.controller "11210000.dwc3"
    setprop sys.usb.configfs 1

on property:ro.bootmode=charger
    setprop sys.usb.config charger

on property:sys.usb.config=charger && property:sys.usb.configfs=1
    write /config/usb_gadget/g1/idVendor 0x18d1
    write /config/usb_gadget/g1/idProduct 0x4eea
    write /config/usb_gadget/g1/configs/b.1/strings/0x409/configuration "midi"
    symlink /config/usb_gadget/g1/functions/midi.gs5 /config/usb_gadget/g1/configs/b.1/f1
    write /config/usb_gadget/g1/UDC ${sys.usb.controller}
    write /sys/class/typec/port0/device/update_sdp_enum_timeout 0x1
    setprop sys.usb.state ${sys.usb.config}

on property:ro.bootmode=usbuwb
    setprop persist.vendor.usb.usbradio.config uwb_acm

on property:vendor.usb.contaminantdisable=true
    exec /vendor/bin/hw/disable_contaminant_detection.sh

on property:persist.vendor.usb.displayport.enabled=1
    write /sys/module/exynos_drm/parameters/dp_enabled 1
    write /sys/class/typec/port0/port0.0/mode1/active "yes"

on property:persist.vendor.usb.displayport.enabled=0
    write /sys/module/exynos_drm/parameters/dp_enabled 0
    write /sys/class/typec/port0/port0.0/mode1/active "no"

on property:persist.sys.hdcp_checking=always
    write /sys/module/exynos_hdcp2/parameters/max_ver 3

on property:persist.sys.hdcp_checking="drm-only"
    write /sys/module/exynos_hdcp2/parameters/max_ver 2

on property:persist.sys.hdcp_checking=never
    write /sys/module/exynos_hdcp2/parameters/max_ver 0
