OSDN Git Service

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