Android USB system initialization init.usb.configfs.rc

After I almost understood init.rc, I saw that there were init.usb.configfs.rc and init.usb.rc files in the directory, which aroused my curiosity. Next, we started to learn about new content.

Android initialization script files are files used to configure and control the Android system startup process. They use a syntax similar to shell scripts, but have some specific commands and variables. The initialization script file is located in the system/core/rootdir directory, starting with init and ending with .rc.

Android USB system initialization refers to setting the corresponding USB configuration and mode according to the type and function of the USB device when the Android system is started to realize the identification and connection of the USB device. Android USB system initialization involves two important initialization script files: init.usb.configfs.rc and init.usb.rc.

1. Detailed explanation of init.usb.configfs.rc file

The init.usb.configfs.rc file is an initialization script file used to configure and control the Android USB system. It uses a configfs (configuration file system)-based method to set USB configuration and mode.

configfs is a special virtual file system that allows users to configure kernel objects such as USB functionality and configuration by creating and modifying files and directories. configfs is usually mounted in the /config directory, and it provides a subdirectory named usb_gadget for managing the properties and behavior of USB devices.

rk3568_r:/config/usb_gadget/g1 $ ls -ll
total 0
-rw-r--r--  1 root root 4096 2023-08-25 10:16:56.268741574 +0800 UDC
-rw-r--r--  1 root root 4096 2023-08-25 10:20:15.795428288 +0800 bDeviceClass
-rw-r--r--  1 root root 4096 2023-08-25 10:20:15.795428288 +0800 bDeviceProtocol
-rw-r--r--  1 root root 4096 2023-08-25 10:20:15.795428288 +0800 bDeviceSubClass
-rw-r--r--  1 root root 4096 2023-08-25 10:20:15.795428288 +0800 bMaxPacketSize0
-rw-r--r--  1 root root 4096 2023-08-24 19:19:58.803334167 +0800 bcdDevice
-rw-r--r--  1 root root 4096 2023-08-24 19:19:58.803334167 +0800 bcdUSB
drwxr-xr-x  3 root root    0 2023-08-24 19:19:58.856667506 +0800 configs
drwxr-xr-x 10 root root    0 2023-08-24 19:19:58.816667502 +0800 functions
-rw-r--r--  1 root root 4096 2023-08-24 19:19:59.033334190 +0800 idProduct
-rw-r--r--  1 root root 4096 2023-08-24 19:19:58.803334167 +0800 idVendor
drwxr-xr-x  2 root root    0 2023-08-24 19:19:59.033334190 +0800 os_desc
drwxr-xr-x  3 root root    0 2023-08-24 19:19:58.803334167 +0800 strings

rk3568_r:/config/usb_gadget/g1 # cat UDC
fcc00000.dwc3
rk3568_r:/config/usb_gadget/g1 # cat bDeviceClass
0x00
rk3568_r:/config/usb_gadget/g1 # cat bDeviceProtocol
0x00
rk3568_r:/config/usb_gadget/g1 # cat bDeviceSubClass
0x00
rk3568_r:/config/usb_gadget/g1 # cat bMaxPacketSize0
0x40
rk3568_r:/config/usb_gadget/g1 # cat bcdDevice
0x0310
rk3568_r:/config/usb_gadget/g1 # cat bcdUSB
0x0200
rk3568_r:/config/usb_gadget/g1 # cat functions
cat: functions: Is a directory
1|rk3568_r:/config/usb_gadget/g1 # cat idProduct
0x0006
rk3568_r:/config/usb_gadget/g1 # cat idProduct
0x0006
rk3568_r:/config/usb_gadget/g1 # cat idVendor
0x2207
rk3568_r:/config/usb_gadget/g1 # cd configs/
rk3568_r:/config/usb_gadget/g1/configs # ls
b.1
rk3568_r:/config/usb_gadget/g1/configs # cd b.1/
rk3568_r:/config/usb_gadget/g1/configs/b.1 # ls
MaxPower  bmAttributes  f1  strings
rk3568_r:/config/usb_gadget/g1/configs/b.1 # cat MaxPower
500
rk3568_r:/config/usb_gadget/g1/configs/b.1 # cat bmAttributes
0x80
rk3568_r:/config/usb_gadget/g1/configs/b.1 # cat strings/0x409/configuration
MaxPower      bmAttributes  f1/           strings/
rk3568_r:/config/usb_gadget/g1/configs/b.1 # cat strings/0x409/configuration
adb
rk3568_r:/config/usb_gadget/g1/configs/b.1 #

The init.usb.configfs.rc file mainly contains the following parts:

  • Property trigger: Property trigger is a mechanism for responding to property changes. It uses on property:... statements to define trigger conditions, such as on property:sys.usb.config=adb means when the sys.usb.config property Fired when the value changes to adb.
on property:sys.usb.config=adb && property:sys.usb.configfs=1
    start adbd
  • Write operation: The write operation is an operation used to modify the content of the file. It uses the write statement to specify the file path and write content, such as write /config/usb_gadget/g1/UDC ${sys.usb.controller}. Write the value of the sys.usb.controller property to the /config/usb_gadget/g1/UDC file.
on property:sys.usb.config=ptp && property:sys.usb.configfs=1
........
    write /config/usb_gadget/g1/UDC ${sys.usb.controller}
........
  • Delete operation: The delete operation is an operation for deleting files or directories. It uses the rm or rmdir statement to specify the file or directory path. For example, rm /config/usb_gadget/g1/configs/b.1/f1 means deleting /config /usb_gadget/g1/configs/b.1/f1 file.
on property:sys.usb.config=none && property:sys.usb.configfs=1
........
    rm /config/usb_gadget/g1/configs/b.1/f1
    rm /config/usb_gadget/g1/configs/b.1/f2
    rm /config/usb_gadget/g1/configs/b.1/f3
........
  • Create operation: The creation operation is an operation for creating a file or directory. It uses the mkdir statement to specify the directory path, such as mkdir /config/usb_gadget/g1/functions/rndis.gs4 means creating /config/usb_gadget/g1/functions /rndis.gs4 directory.
on property:sys.usb.config=rndis && property:sys.usb.configfs=1
........
    mkdir /config/usb_gadget/g1/functions/rndis.gs4
........
  • Link operation: The link operation is an operation used to create a symbolic link. It uses the symlink statement to specify the source file and target file paths, such as symlink /config/usb_gadget/g1/functions/rndis.gs4 /config/usb_gadget/g1/ configs/b.1/f1 means linking /config/usb_gadget/g1/functions/rndis.gs4 to /config/usb_gadget/g1/configs/b.1/f1.
on property:sys.usb.config=rndis && property:sys.usb.configfs=1
........
    symlink /config/usb_gadget/g1/functions/rndis.gs4 
........
  • Start operation: The start operation is an operation for starting a service. It uses the start statement to specify the service name. For example, start adbd means starting the adbd service.
on property:sys.usb.config=adb && property:sys.usb.configfs=1
    start adbd
  • Stop operation: The stop operation is an operation used to stop a service. It uses the stop statement to specify the service name. For example, stop adbd means stopping the adbd service.
........
on property:sys.usb.config=none && property:sys.usb.configfs=1
    stop adbd
........
  • Set attribute operation: The set attribute operation is an operation for setting attribute values. It uses the setprop statement to specify the attribute name and attribute value. For example, setprop sys.usb.state ${sys.usb.config} means setting sys.usb The value of the .state property is set to the value of the sys.usb.config property.
on property:sys.usb.config=none && property:sys.usb.configfs=1
........
    setprop sys.usb.state ${sys.usb.config}
........

The main function of the init.usb.configfs.rc file is to create and delete corresponding USB functions and configuration files according to different USB configurations, and to start and stop corresponding services. Each attribute trigger in this file corresponds to a USB configuration, such as adb, mtp, ptp, accessory, etc. When the value of the sys.usb.config attribute changes, the corresponding operation will be performed, such as writing, Delete, create, link, start, stop, set properties, etc. These operations are implemented through the configfs file system, and they will affect the identification and connection of USB devices.

2. Overview of init.usb.configfs.rc file

The structure of the init.usb.configfs.rc file is roughly as follows:

on property:sys.usb.config=none && property:sys.usb.configfs=1
    # 无USB配置时的操作
    ...

on property:sys.usb.config=adb && property:sys.usb.configfs=1
    # adb USB配置时的操作
    ...

on property:sys.usb.ffs.ready=1 && property:sys.usb.config=adb && property:sys.usb.configfs=1
    # adb USB配置并且ffs服务准备好时的操作
    ...

on property:sys.usb.config=mtp && property:sys.usb.configfs=1
    # mtp USB配置时的操作
    ...

on property:sys.usb.config=mtp,adb && property:sys.usb.configfs=1
    # mtp,adb USB配置时的操作
    ...

on property:sys.usb.ffs.ready=1 && property:sys.usb.config=mtp,adb && property:sys.usb.configfs=1
    # mtp,adb USB配置并且ffs服务准备好时的操作
    ...

on property:sys.usb.config=ptp && property:sys.usb.configfs=1
    # ptp USB配置时的操作
    ...

on property:sys.usb.config=ptp,adb && property:sys.usb.configfs=1
    # ptp,adb USB配置时的操作
    ...

on property:sys.usb.ffs.ready=1 && property:sys.usb.config=ptp,adb && property:sys.usb.configfs=1
    # ptp,adb USB配置并且ffs服务准备好时的操作
    ...

on property:sys.usb.config=accessory && property:sys.usb.configfs=1
    # accessory USB配置时的操作
    ...

on property:sys.usb.config=accessory,adb && property:sys.usb.configfs=1
    # accessory,adb USB配置时的操作
    ...

on property:sys.usb.ffs.ready=1 && property:sys.usb.config=accessory,adb && property:sys.usb.configfs=1
    # accessory,adb USB配置并且ffs服务准备好时的操作
    ...

on property:sys.usb.config=audio_source && property:sys.usb.configfs=1
    # audio_source USB配置时的操作
    ...

on property:sys.usb.config=audio_source,adb && property:sys.usb.configfs=1
    # audio_source,adb USB配置时的操作
    ...

on property:sys.usb.ffs.ready=1 && property:sys.usb.config=audio_source,adb && property:sys.usb.configfs=1
    # audio_source,adb USB配置并且ffs服务准备好时的操作
    ...

on property:sys.usb.config=accessory,audio_source && property:sys.usb.configfs=1
    # accessory,audio_source USB配置时的操作
    ...

on property:sys.usb.config=accessory,audio_source,adb && property:sys.usb.configfs=1
    # accessory,audio_source,adb USB配置时的操作
    ...

on property:sys.usb.ffs.ready=1 && property:sys.usb.config=accessory,audio_source,adb && property:sys.usb.configfs=1
    # accessory,audio_source,adb USB配置并且ffs服务准备好时的操作
    ...

on property:sys.usb.config=midi && property:sys.usb.configfs=1
    # midi USB配置时的操作
    ...

on property:sys.usb.config=midi,adb && property:sys.usb.configfs=1
    # midi,adb USB配置时的操作
    ...

on property:sys.usb.ffs.ready=1 && property:sys.usb.config=midi,adb && property:sys.usb.configfs=1
    # midi,adb USB配置并且ffs服务准备好时的操作
    ...

on property:sys.usb.config=rndis && property:sys.usb.configfs=1
    # rndis USB配置时的操作
    ...

on property:sys.usb.config=rndis,adb && property:sys.usb.configfs=1
    # rndis,adb USB配置时的操作
    ...

on property:sys.usb.ffs.ready=1 && property:sys.usb.config=rndis,adb && property:sys.usb.configfs=1
    # rndis,adb USB配置并且ffs服务准备好时的操作
    ...

It can be seen that each attribute trigger in this file is defined according to different USB configurations, and each USB configuration has two situations: one is the operation when only the USB configuration changes, and the other is What to do when the USB configuration changes and the ffs service (i.e. the service required for adb functionality) is ready. This is because the adb function needs to wait for the ffs service to start before it can work properly, so it needs to be handled separately.

3. init.usb.configfs.rc detailed syntax and commands

The following will take adb USB configuration as an example to learn the syntax and commands in the init.usb.configfs.rc file in detail.

3.1 Operations during adb USB configuration

When the value of the sys.usb.config property changes to adb, the following actions are performed:

on property:sys.usb.config=adb && property:sys.usb.configfs=1
    start adbd

There is only one command here, which is to start the adbd service. The adbd service is the service responsible for implementing the adb function. It creates a file named ffs.adb and mounts it to the /data/adb directory. This file is the interface used to communicate with the adb client on the PC. It will be recognized by the configfs file system as a USB function.

rk3568_r:/data/adb # ls -ll
total 0

3.2 Operation when adb USB is configured and ffs service is ready

When the value of the sys.usb.config property changes to adb, and the value of the sys.usb.ffs.ready property changes to 1, the following actions are performed:

on property:sys.usb.ffs.ready=1 && property:sys.usb.config=adb && property:sys.usb.configfs=1
    write /config/usb_gadget/g1/configs/b.1/strings/0x409/configuration "adb"
    symlink /config/usb_gadget/g1/functions/ffs.adb /config/usb_gadget/g1/configs/b.1/f1
    write /config/usb_gadget/g1/UDC ${sys.usb.controller}
    setprop sys.usb.state ${sys.usb.config}

There are four commands here, namely:

  • write /config/usb_gadget/g1/configs/b.1/strings/0x409/configuration "adb": This command writes the "adb" string to /config/usb_gadget/g1/configs/b.1/strings/ 0x409/configuration file. This file is used to set the name of the USB configuration. 0x409 indicates the English language code, and "adb" indicates that this USB configuration only contains one function, the adb function.

  • symlink /config/usb_gadget/g1/functions/ffs.adb /config/usb_gadget/g1/configs/b.1/f1: This command links the /config/usb_gadget/g1/functions/ffs.adb file to /config /usb_gadget/g1/configs/b.1/f1 file. This command is used to associate USB functions with USB configuration. The ffs.adb file is the USB function file created by the adbd service. The f1 file is the file representing the first function in the USB configuration.

  • write /config/usb_gadget/g1/UDC ${sys.usb.controller}: This command writes the value of the sys.usb.controller attribute into the /config/usb_gadget/g1/UDC file. This command is used to activate USB devices. The UDC file is a file representing the USB device controller (Universal Device Controller). ${sys.usb.controller} represents the name of the currently used USB
    controller, such as dwc3.0 or ehci. .0 etc.

  • setprop sys.usb.state ${sys.usb.config}: This command sets the value of the sys.usb.config property to the value of the sys.usb.state property. This command is used to update the USB status. The sys.usb.state attribute represents the current USB status, which should be consistent with the sys.usb.config attribute.

The execution result of these four commands is that the USB device will be recognized as a USB device with only adb function. The adb client on the PC can communicate with the adbd service through the ffs.adb file.

Section

The above is a detailed explanation of the init.usb.configfs.rc file. You can refer to other USB configurations in this file to understand their functions and syntax.

Guess you like

Origin blog.csdn.net/SHH_1064994894/article/details/132352206
usb
usb
Recommended