OSDN Git Service

Port to Android 11
[android-x86/device-generic-common.git] / init.sh
1 #
2 # Copyright (C) 2013-2018 The Android-x86 Open Source Project
3 #
4 # License: GNU Public License v2 or later
5 #
6
7 function set_property()
8 {
9         setprop "$1" "$2"
10         [ -n "$DEBUG" ] && echo "$1"="$2" >> /dev/x86.prop
11 }
12
13 function set_prop_if_empty()
14 {
15         [ -z "$(getprop $1)" ] && set_property "$1" "$2"
16 }
17
18 function rmmod_if_exist()
19 {
20         for m in $*; do
21                 [ -d /sys/module/$m ] && rmmod $m
22         done
23 }
24
25 function init_misc()
26 {
27         # device information
28         setprop ro.product.manufacturer "$(cat $DMIPATH/sys_vendor)"
29         setprop ro.product.model "$PRODUCT"
30
31         # a hack for USB modem
32         lsusb | grep 1a8d:1000 && eject
33
34         # in case no cpu governor driver autoloads
35         [ -d /sys/devices/system/cpu/cpu0/cpufreq ] || modprobe acpi-cpufreq
36
37         # enable sdcardfs if /data is not mounted on tmpfs or 9p
38         mount | grep /data\ | grep -qE 'tmpfs|9p'
39         [ $? -eq 0 ] && set_prop_if_empty ro.sys.sdcardfs false
40
41         # remove wl if it's not used
42         local wifi
43         if [ -d /sys/class/net/wlan0 ]; then
44                 wifi=$(basename `readlink /sys/class/net/wlan0/device/driver`)
45                 [ "$wifi" != "wl" ] && rmmod_if_exist wl
46         fi
47
48         # enable virt_wifi if needed
49         local eth=`getprop net.virt_wifi eth0`
50         if [ -d /sys/class/net/$eth -a "$VIRT_WIFI" != "0" ]; then
51                 if [ -n "$wifi" -a "$VIRT_WIFI" = "1" ]; then
52                         rmmod_if_exist iwlmvm $wifi
53                 fi
54                 if [ ! -d /sys/class/net/wlan0 ]; then
55                         ifconfig $eth down
56                         ip link set $eth name wifi_eth
57                         ifconfig wifi_eth up
58                         ip link add link wifi_eth name wlan0 type virt_wifi
59                 fi
60         fi
61 }
62
63 function init_hal_audio()
64 {
65         case "$PRODUCT" in
66                 VirtualBox*|Bochs*)
67                         [ -d /proc/asound/card0 ] || modprobe snd-sb16 isapnp=0 irq=5
68                         ;;
69                 TS10*)
70                         set_prop_if_empty hal.audio.out pcmC0D2p
71                         ;;
72         esac
73 }
74
75 function init_hal_bluetooth()
76 {
77         for r in /sys/class/rfkill/*; do
78                 type=$(cat $r/type)
79                 [ "$type" = "wlan" -o "$type" = "bluetooth" ] && echo 1 > $r/state
80         done
81
82         case "$PRODUCT" in
83                 T100TAF)
84                         set_property bluetooth.interface hci1
85                         ;;
86                 T10*TA|M80TA|HP*Omni*)
87                         BTUART_PORT=/dev/ttyS1
88                         set_property hal.bluetooth.uart.proto bcm
89                         ;;
90                 MacBookPro8*)
91                         rmmod b43
92                         modprobe b43 btcoex=0
93                         modprobe btusb
94                         ;;
95                 # FIXME
96                 # Fix MacBook 2013-2015 (Air6/7&Pro11/12) BCM4360 ssb&wl conflict.
97                 MacBookPro11* | MacBookPro12* | MacBookAir6* | MacBookAir7*)
98                         rmmod b43
99                         rmmod ssb
100                         rmmod bcma
101                         rmmod wl
102                         modprobe wl
103                         modprobe btusb
104                         ;;
105                 *)
106                         for bt in $(busybox lsusb -v | awk ' /Class:.E0/ { print $9 } '); do
107                                 chown 1002.1002 $bt && chmod 660 $bt
108                         done
109                         ;;
110         esac
111
112         if [ -n "$BTUART_PORT" ]; then
113                 set_property hal.bluetooth.uart $BTUART_PORT
114                 chown bluetooth.bluetooth $BTUART_PORT
115                 start btattach
116         fi
117
118         # rtl8723bs bluetooth
119         if dmesg -t | grep -qE '8723bs.*BT'; then
120                 TTYSTRING=`dmesg -t | grep -E 'tty.*MMIO' | awk '{print $2}' | head -1`
121                 if [ -n "$TTYSTRING" ]; then
122                         echo "RTL8723BS BT uses $TTYSTRING for Bluetooth."
123                         ln -sf $TTYSTRING /dev/rtk_h5
124                         start rtk_hciattach
125                 fi
126         fi
127 }
128
129 function init_hal_camera()
130 {
131         case "$UEVENT" in
132                 *81EK*)
133                         set_prop_if_empty hal.camera.0 0,0
134                         ;;
135                 *e-tabPro*)
136                         set_prop_if_empty hal.camera.0 0,270
137                         set_prop_if_empty hal.camera.2 1,90
138                         ;;
139                 *LenovoideapadD330*)
140                         set_prop_if_empty hal.camera.0 0,90
141                         set_prop_if_empty hal.camera.2 1,90
142                         ;;
143                 *)
144                         ;;
145         esac
146 }
147
148 function init_hal_gps()
149 {
150         # TODO
151         return
152 }
153
154 function set_drm_mode()
155 {
156         case "$PRODUCT" in
157                 ET1602*)
158                         drm_mode=1366x768
159                         ;;
160                 *)
161                         [ -n "$video" ] && drm_mode=$video
162                         ;;
163         esac
164
165         [ -n "$drm_mode" ] && set_property debug.drm.mode.force $drm_mode
166 }
167
168 function init_uvesafb()
169 {
170         UVESA_MODE=${UVESA_MODE:-${video%@*}}
171
172         case "$PRODUCT" in
173                 ET2002*)
174                         UVESA_MODE=${UVESA_MODE:-1600x900}
175                         ;;
176                 *)
177                         ;;
178         esac
179
180         modprobe uvesafb mode_option=${UVESA_MODE:-1024x768}-32 ${UVESA_OPTION:-mtrr=3 scroll=redraw} v86d=/system/bin/v86d
181 }
182
183 function init_hal_gralloc()
184 {
185         [ "$VULKAN" = "1" ] && GRALLOC=gbm
186
187         case "$(readlink /sys/class/graphics/fb0/device/driver)" in
188                 *virtio_gpu)
189                         HWC=${HWC:-drm}
190                         GRALLOC=${GRALLOC:-gbm}
191                         video=${video:-1280x768}
192                         ;&
193                 *i915|*radeon|*nouveau|*vmwgfx|*amdgpu)
194                         if [ "$HWACCEL" != "0" ]; then
195                                 ${HWC:+set_property ro.hardware.hwcomposer $HWC}
196                                 set_property ro.hardware.gralloc ${GRALLOC:-drm}
197                                 set_drm_mode
198                         fi
199                         ;;
200                 "")
201                         init_uvesafb
202                         ;&
203                 *)
204                         ;;
205         esac
206
207         [ -z "$(getprop ro.hardware.gralloc)" ] && set_property ro.hardware.egl swiftshader
208         [ -n "$DEBUG" ] && set_property debug.egl.trace error
209 }
210
211 function init_hal_hwcomposer()
212 {
213         # TODO
214         [ "$HWC" = "drmfb" ] && start vendor.hwcomposer-2-1.drmfb
215         return
216 }
217
218 function init_hal_vulkan()
219 {
220         case "$(readlink /sys/class/graphics/fb0/device/driver)" in
221                 *i915)
222                         set_property ro.hardware.vulkan android-x86
223                         ;;
224                 *amdgpu)
225                         set_property ro.hardware.vulkan radv
226                         ;;
227                 *)
228                         ;;
229         esac
230 }
231
232 function init_hal_lights()
233 {
234         chown 1000.1000 /sys/class/backlight/*/brightness
235 }
236
237 function init_hal_power()
238 {
239         for p in /sys/class/rtc/*; do
240                 echo disabled > $p/device/power/wakeup
241         done
242
243         # TODO
244         case "$PRODUCT" in
245                 HP*Omni*|OEMB|Standard*PC*|Surface*3|T10*TA|VMware*)
246                         set_prop_if_empty sleep.state none
247                         ;;
248                 e-tab*Pro)
249                         set_prop_if_empty sleep.state force
250                         ;;
251                 *)
252                         ;;
253         esac
254 }
255
256 function init_hal_sensors()
257 {
258         # if we have sensor module for our hardware, use it
259         ro_hardware=$(getprop ro.hardware)
260         [ -f /system/lib/hw/sensors.${ro_hardware}.so ] && return 0
261
262         local hal_sensors=kbd
263         local has_sensors=true
264         case "$UEVENT" in
265                 *Lucid-MWE*)
266                         set_property ro.ignore_atkbd 1
267                         hal_sensors=hdaps
268                         ;;
269                 *ICONIA*W5*)
270                         hal_sensors=w500
271                         ;;
272                 *S10-3t*)
273                         hal_sensors=s103t
274                         ;;
275                 *Inagua*)
276                         #setkeycodes 0x62 29
277                         #setkeycodes 0x74 56
278                         set_property ro.ignore_atkbd 1
279                         set_property hal.sensors.kbd.type 2
280                         ;;
281                 *TEGA*|*2010:svnIntel:*)
282                         set_property ro.ignore_atkbd 1
283                         set_property hal.sensors.kbd.type 1
284                         io_switch 0x0 0x1
285                         setkeycodes 0x6d 125
286                         ;;
287                 *DLI*)
288                         set_property ro.ignore_atkbd 1
289                         set_property hal.sensors.kbd.type 1
290                         setkeycodes 0x64 1
291                         setkeycodes 0x65 172
292                         setkeycodes 0x66 120
293                         setkeycodes 0x67 116
294                         setkeycodes 0x68 114
295                         setkeycodes 0x69 115
296                         setkeycodes 0x6c 114
297                         setkeycodes 0x6d 115
298                         ;;
299                 *tx2*)
300                         setkeycodes 0xb1 138
301                         setkeycodes 0x8a 152
302                         set_property hal.sensors.kbd.type 6
303                         set_property poweroff.doubleclick 0
304                         set_property qemu.hw.mainkeys 1
305                         ;;
306                 *MS-N0E1*)
307                         set_property ro.ignore_atkbd 1
308                         set_property poweroff.doubleclick 0
309                         setkeycodes 0xa5 125
310                         setkeycodes 0xa7 1
311                         setkeycodes 0xe3 142
312                         ;;
313                 *Aspire1*25*)
314                         modprobe lis3lv02d_i2c
315                         echo -n "enabled" > /sys/class/thermal/thermal_zone0/mode
316                         ;;
317                 *ThinkPad*Tablet*)
318                         modprobe hdaps
319                         hal_sensors=hdaps
320                         ;;
321                 *LenovoideapadD330*)
322                         set_property ro.iio.accel.quirks no-trig
323                         set_property ro.iio.accel.order 102
324                         ;&
325                 *LINX1010B*)
326                         set_property ro.iio.accel.x.opt_scale -1
327                         set_property ro.iio.accel.z.opt_scale -1
328                         ;;
329                 *i7-WN*)
330                         set_property ro.iio.accel.quirks no-trig
331                         ;&
332                 *i7Stylus*)
333                         set_property ro.iio.accel.x.opt_scale -1
334                         ;;
335                 *LenovoMIIX320*|*ONDATablet*)
336                         set_property ro.iio.accel.order 102
337                         set_property ro.iio.accel.x.opt_scale -1
338                         set_property ro.iio.accel.y.opt_scale -1
339                         ;;
340                 *SP111-33*|*TP300L*)
341                         set_property ro.iio.accel.quirks no-trig
342                         ;&
343                 *ST70416-6*)
344                         set_property ro.iio.accel.order 102
345                         ;;
346                 *e-tabPro*|*pnEZpad*|*TECLAST:rntPAD*)
347                         set_property ro.iio.accel.quirks no-trig
348                         ;&
349                 *T*0*TA*|*M80TA*)
350                         set_property ro.iio.accel.y.opt_scale -1
351                         ;;
352                 *)
353                         has_sensors=false
354                         ;;
355         esac
356
357         # has iio sensor-hub?
358         if [ -n "`ls /sys/bus/iio/devices/iio:device* 2> /dev/null`" ]; then
359                 busybox chown -R 1000.1000 /sys/bus/iio/devices/iio:device*/
360                 [ -n "`ls /sys/bus/iio/devices/iio:device*/in_accel_x_raw 2> /dev/null`" ] && has_sensors=true
361                 hal_sensors=iio
362         elif lsmod | grep -q lis3lv02d_i2c; then
363                 hal_sensors=hdaps
364                 has_sensors=true
365         elif [ "$hal_sensors" != "kbd" ]; then
366                 has_sensors=true
367         fi
368
369         set_property ro.hardware.sensors $hal_sensors
370         set_property config.override_forced_orient ${HAS_SENSORS:-$has_sensors}
371 }
372
373 function create_pointercal()
374 {
375         if [ ! -e /data/misc/tscal/pointercal ]; then
376                 mkdir -p /data/misc/tscal
377                 touch /data/misc/tscal/pointercal
378                 chown 1000.1000 /data/misc/tscal /data/misc/tscal/*
379                 chmod 775 /data/misc/tscal
380                 chmod 664 /data/misc/tscal/pointercal
381         fi
382 }
383
384 function init_tscal()
385 {
386         case "$UEVENT" in
387                 *ST70416-6*)
388                         modprobe gslx680_ts_acpi
389                         ;&
390                 *81EK*|*T91**|*T101*|*ET2002*|*74499FU*|*945GSE-ITE8712*|*CF-19[CDYFGKLP]*|*TECLAST:rntPAD*)
391                         create_pointercal
392                         return
393                         ;;
394                 *)
395                         ;;
396         esac
397
398         for usbts in $(lsusb | awk '{ print $6 }'); do
399                 case "$usbts" in
400                         0596:0001|0eef:0001)
401                                 create_pointercal
402                                 return
403                                 ;;
404                         *)
405                                 ;;
406                 esac
407         done
408 }
409
410 function init_ril()
411 {
412         case "$UEVENT" in
413                 *TEGA*|*2010:svnIntel:*|*Lucid-MWE*)
414                         set_property rild.libpath /system/lib/libhuaweigeneric-ril.so
415                         set_property rild.libargs "-d /dev/ttyUSB2 -v /dev/ttyUSB1"
416                         set_property ro.radio.noril no
417                         ;;
418                 *)
419                         set_property ro.radio.noril yes
420                         ;;
421         esac
422 }
423
424 function init_cpu_governor()
425 {
426         governor=$(getprop cpu.governor)
427
428         [ $governor ] && {
429                 for cpu in $(ls -d /sys/devices/system/cpu/cpu?); do
430                         echo $governor > $cpu/cpufreq/scaling_governor || return 1
431                 done
432         }
433 }
434
435 function do_init()
436 {
437         init_misc
438         init_hal_audio
439         init_hal_bluetooth
440         init_hal_camera
441         init_hal_gps
442         init_hal_gralloc
443         init_hal_hwcomposer
444         init_hal_vulkan
445         init_hal_lights
446         init_hal_power
447         init_hal_sensors
448         init_tscal
449         init_ril
450         post_init
451 }
452
453 function do_netconsole()
454 {
455         modprobe netconsole netconsole="@/,@$(getprop dhcp.eth0.gateway)/"
456 }
457
458 function do_bootcomplete()
459 {
460         hciconfig | grep -q hci || pm disable com.android.bluetooth
461
462         init_cpu_governor
463
464         [ -z "$(getprop persist.sys.root_access)" ] && setprop persist.sys.root_access 3
465
466         lsmod | grep -Ehq "brcmfmac|rtl8723be" && setprop wlan.no-unload-driver 1
467
468         case "$PRODUCT" in
469                 1866???|1867???|1869???) # ThinkPad X41 Tablet
470                         start tablet-mode
471                         start wacom-input
472                         setkeycodes 0x6d 115
473                         setkeycodes 0x6e 114
474                         setkeycodes 0x69 28
475                         setkeycodes 0x6b 158
476                         setkeycodes 0x68 172
477                         setkeycodes 0x6c 127
478                         setkeycodes 0x67 217
479                         ;;
480                 6363???|6364???|6366???) # ThinkPad X60 Tablet
481                         ;&
482                 7762???|7763???|7767???) # ThinkPad X61 Tablet
483                         start tablet-mode
484                         start wacom-input
485                         setkeycodes 0x6d 115
486                         setkeycodes 0x6e 114
487                         setkeycodes 0x69 28
488                         setkeycodes 0x6b 158
489                         setkeycodes 0x68 172
490                         setkeycodes 0x6c 127
491                         setkeycodes 0x67 217
492                         ;;
493                 7448???|7449???|7450???|7453???) # ThinkPad X200 Tablet
494                         start tablet-mode
495                         start wacom-input
496                         setkeycodes 0xe012 158
497                         setkeycodes 0x66 172
498                         setkeycodes 0x6b 127
499                         ;;
500                 Surface*Go)
501                         echo on > /sys/devices/pci0000:00/0000:00:15.1/i2c_designware.1/power/control
502                         ;;
503                 VMware*)
504                         pm disable com.android.bluetooth
505                         ;;
506                 X80*Power)
507                         set_property power.nonboot-cpu-off 1
508                         ;;
509                 *)
510                         ;;
511         esac
512
513 #       [ -d /proc/asound/card0 ] || modprobe snd-dummy
514         for c in $(grep '\[.*\]' /proc/asound/cards | awk '{print $1}'); do
515                 f=/system/etc/alsa/$(cat /proc/asound/card$c/id).state
516                 if [ -e $f ]; then
517                         alsa_ctl -f $f restore $c
518                 else
519                         alsa_ctl init $c
520                         alsa_amixer -c $c set Master on
521                         alsa_amixer -c $c set Master 100%
522                         alsa_amixer -c $c set Headphone on
523                         alsa_amixer -c $c set Headphone 100%
524                         alsa_amixer -c $c set Speaker 100%
525                         alsa_amixer -c $c set Capture 80%
526                         alsa_amixer -c $c set Capture cap
527                         alsa_amixer -c $c set PCM 100% unmute
528                         alsa_amixer -c $c set SPO unmute
529                         alsa_amixer -c $c set IEC958 on
530                         alsa_amixer -c $c set 'Mic Boost' 1
531                         alsa_amixer -c $c set 'Internal Mic Boost' 1
532                 fi
533         done
534
535         post_bootcomplete
536 }
537
538 PATH=/sbin:/system/bin:/system/xbin
539
540 DMIPATH=/sys/class/dmi/id
541 BOARD=$(cat $DMIPATH/board_name)
542 PRODUCT=$(cat $DMIPATH/product_name)
543 UEVENT=$(cat $DMIPATH/uevent)
544
545 # import cmdline variables
546 for c in `cat /proc/cmdline`; do
547         case $c in
548                 BOOT_IMAGE=*|iso-scan/*|*.*=*)
549                         ;;
550                 *=*)
551                         eval $c
552                         if [ -z "$1" ]; then
553                                 case $c in
554                                         DEBUG=*)
555                                                 [ -n "$DEBUG" ] && set_property debug.logcat 1
556                                                 [ "$DEBUG" = "0" ] || SETUPWIZARD=${SETUPWIZARD:-0}
557                                                 ;;
558                                         DPI=*)
559                                                 set_property ro.sf.lcd_density "$DPI"
560                                                 ;;
561                                 esac
562                                 [ "$SETUPWIZARD" = "0" ] && set_property ro.setupwizard.mode DISABLED
563                         fi
564                         ;;
565         esac
566 done
567
568 [ -n "$DEBUG" ] && set -x || exec &> /dev/null
569
570 # import the vendor specific script
571 hw_sh=/vendor/etc/init.sh
572 [ -e $hw_sh ] && source $hw_sh
573
574 case "$1" in
575         netconsole)
576                 [ -n "$DEBUG" ] && do_netconsole
577                 ;;
578         bootcomplete)
579                 do_bootcomplete
580                 ;;
581         init|"")
582                 do_init
583                 ;;
584 esac
585
586 return 0