This commit was manufactured by cvs2svn to create branch
[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 echo "pl_hwinit: loading floppy device driver"
53 /sbin/modprobe floppy
54
55 # always wait a bit between loading the usb drivers, and checking /sys/
56 # for usb devices (this isn't necessarily for waiting for mass storage files,
57 # that is done below)
58 echo "pl_hwinit: waiting for usb system to initialize."
59 /bin/sleep 10s
60
61 # sometimes, flash devices take a while to initialize. in fact, the kernel
62 # intentionally waits 5 seconds for a device to 'settle'. some take even longer
63 # to show up. if there are any mass storage devices on the system, try to
64 # delay until they come online, up to a max delay of 30s.
65
66 # the way this will be done is to look for files in /sys/devices that are named
67 # 'bInterfaceClass', these will be a list of the usb devices on the system, and
68 # their primary usb device interface class ids. The base directory these files 
69 # exist in will be the full path to the /sys/device entry for that device.
70 # for each mass storage devices (they have an interface class value of 08),
71 # we wait for a new symbolic link named 'driver' to exist in that directory,
72 # indicating the kernel loaded a driver for that device.
73
74 # usb interface class id for mass storage
75 INTERFACE_CLASS_MASS_STORAGE="08"
76
77 # how long to wait in seconds before continuing on if devices
78 # aren't available
79 MAX_USB_WAIT_TIME=30
80
81 # low long in seconds to wait between checks
82 PER_CHECK_USB_WAIT_TIME=5
83
84
85 # find out if the device identified by the /sys dir has a module
86 # loaded for it. check for a symlink in the dir named driver.
87 function does_device_dir_have_driver()
88 {
89     if [[ -h "$1/driver" ]]; then
90         return 1
91     else
92         return 0
93     fi
94 }
95
96 wait_dev_list=""
97 for interface_class_file in `find /sys/devices -name 'bInterfaceClass'`; do
98     interface_class=`cat $interface_class_file`
99     if [[ "$interface_class" == $INTERFACE_CLASS_MASS_STORAGE ]]; then
100         wait_dev_list="$wait_dev_list "`dirname $interface_class_file`
101     fi
102 done
103
104 if [[ -n "$wait_dev_list" ]]; then
105     echo "pl_hwinit: found USB mass storage device(s). Attempting to wait"
106     echo "pl_hwinit: up to $MAX_USB_WAIT_TIME seconds for them to come online."
107
108     total_wait_time=0
109     success=0
110     while [[ $total_wait_time < $MAX_USB_WAIT_TIME ]]; do
111         
112         total_wait_time=$(($total_wait_time+$PER_CHECK_USB_WAIT_TIME))
113
114         echo "pl_hwinit: waiting $PER_CHECK_USB_WAIT_TIME seconds."
115         /bin/sleep $PER_CHECK_USB_WAIT_TIME
116
117         all_devices_online=1
118         for device_dir in $wait_dev_list; do
119             does_device_dir_have_driver $device_dir
120             if [[ "$?" -eq 0 ]]; then
121                 all_devices_online=0
122             fi
123         done
124
125         if [[ $all_devices_online -eq 1 ]]; then
126             success=1
127             echo "pl_hwinit: looks like the devices are now online."
128             break;
129         else
130             echo "pl_hwinit: not all devices online yet, waiting..."
131         fi
132     done
133
134     if [[ $success -eq 1 ]]; then
135         echo "pl_hwinit: Succesfully waited for USB mass storage devices"
136         echo "pl_hwinit: to come online."
137     else
138         echo "pl_hwinit: One or more USB mass storage devices did not"
139         echo "pl_hwinit: initialize in time. Continuing anyway."
140     fi
141 fi
142