- move flash device delay from pl_netinit to pl_hwinit
[bootcd.git] / conf_files / pl_hwinit
1 #!/bin/sh
2
3 pci_table=/etc/pl_pcitable
4
5 loaded_module_list=/tmp/loadedmodules
6
7 echo "pl_hwinit: loading applicable modules"
8
9 echo > $loaded_module_list
10
11 # this will contain lines of device_id:vendor_id (no 0x)
12 system_devices=$(lspci -n | cut -d " " -f4)
13
14 for device in $system_devices; do
15
16     # now vendor_id and device_id are broken apart
17     vendor_id=$(echo $device | cut -d ":" -f1)
18     device_id=$(echo $device | cut -d ":" -f2)
19
20     # either exactly match vendor:device, or a vendor:ffff (let the module
21     # figure out if it can be used for this device), or ffff:device
22     # (not sure if this is legal, but shows up in the pci map)
23     mods=$(grep -i "\($vendor_id:ffff\|$vendor_id:$device_id\|ffff:$device_id\)" \
24         $pci_table | cut -d " " -f1)
25
26     for module in $mods; do
27         if [ -n "$module" ]; then
28             echo "pl_hwinit: found and loading module $module"
29             /sbin/modprobe $module
30             echo $module >> $loaded_module_list
31         fi
32     done
33 done
34
35 # just in case, look for any modules that are ffff:ffff and load them
36 mods=$(grep -i "ffff:ffff" $pci_table | cut -d " " -f1)
37 for module in $mods; do
38     if [ -n "$module" ]; then
39         echo "pl_hwinit: found and loading wild module $module"
40         /sbin/modprobe $module
41     fi
42 done
43
44 # sd_mod won't get loaded automatically
45 echo "pl_hwinit: loading sd_mod"
46 /sbin/modprobe sd_mod
47
48 # load usb_storage to support node conf files on flash disks
49 echo "pl_hwinit: loading usb_storage"
50 /sbin/modprobe usb_storage
51
52 # sometimes, flash devices take a while to initialize. in fact, the kernel
53 # intentionally waits 5 seconds for a device to 'settle'. some take even longer
54 # to show up. if there are any mass storage devices on the system, try to
55 # delay until they come online, up to a max delay of 30s.
56
57 # the way this will be done is to look for files in /sys/devices that are named
58 # 'bInterfaceClass', these will be a list of the usb devices on the system, and
59 # their primary usb device interface class ids. The base directory these files 
60 # exist in will be the full path to the /sys/device entry for that device.
61 # for each mass storage devices (they have an interface class value of 08),
62 # we wait for a new symbolic link named 'driver' to exist in that directory,
63 # indicating the kernel loaded a driver for that device.
64
65 # usb interface class id for mass storage
66 INTERFACE_CLASS_MASS_STORAGE="08"
67
68 # how long to wait in seconds before continuing on if devices
69 # aren't available
70 MAX_USB_WAIT_TIME=30
71
72 # low long in seconds to wait between checks
73 PER_CHECK_USB_WAIT_TIME=5
74
75
76 # find out if the device identified by the /sys dir has a module
77 # loaded for it. check for a symlink in the dir named driver.
78 function does_device_dir_have_driver()
79 {
80     if [[ -h "$1/driver" ]]; then
81         return 1
82     else
83         return 0
84     fi
85 }
86
87 wait_dev_list=""
88 for interface_class_file in `/bin/find /sys/devices -name 'bInterfaceClass'`; do
89     interface_class=`cat $interface_class_file`
90     if [[ "$interface_class" == $INTERFACE_CLASS_MASS_STORAGE ]]; then
91         wait_dev_list="$wait_dev_list "`/bin/dirname $interface_class_file`
92     fi
93 done
94
95 if [[ -n "$wait_dev_list" ]]; then
96     echo "pl_hwinit: found USB mass storage device(s). Attempting to wait"
97     echo "pl_hwinit: up to $MAX_USB_WAIT_TIME seconds for them to come online."
98
99     total_wait_time=0
100     success=0
101     while [[ $total_wait_time < $MAX_USB_WAIT_TIME ]]; do
102         
103         total_wait_time=$(($total_wait_time+$PER_CHECK_USB_WAIT_TIME))
104
105         echo "pl_hwinit: waiting $PER_CHECK_USB_WAIT_TIME seconds."
106         sleep $PER_CHECK_USB_WAIT_TIME
107
108         all_devices_online=1
109         for device_dir in $wait_dev_list; do
110             does_device_dir_have_driver $device_dir
111             if [[ "$?" -eq 0 ]]; then
112                 all_devices_online=0
113             fi
114         done
115
116         if [[ $all_devices_online -eq 1 ]]; then
117             success=1
118             echo "pl_hwinit: looks like the devices are now online."
119             break;
120         else
121             echo "pl_hwinit: not all devices online yet, waiting..."
122         fi
123     done
124
125     if [[ $success -eq 1 ]]; then
126         echo "pl_hwinit: Succesfully waited for USB mass storage devices"
127         echo "pl_hwinit: to come online."
128     else
129         echo "pl_hwinit: One or more USB mass storage devices did not"
130         echo "pl_hwinit: initialize in time. Continuing anyway."
131     fi
132 fi
133
134 echo "pl_hwinit: loading floppy device driver"
135 /sbin/modprobe floppy