#! /bin/sh
#
# Laptop mode tools module: bluetooth
#

if [ x$CONTROL_BLUETOOTH = x1 ] ; then
	ENABLE_BLUETOOTH=$BATT_ENABLE_BLUETOOTH
	if [ $ON_AC -eq 1 ] ; then
		ENABLE_BLUETOOTH=$AC_ENABLE_BLUETOOTH
	fi
	if [ x$ENABLE_BLUETOOTH = x1 ] ; then
		log "VERBOSE" "Enabling bluetooth."
		modprobe -q hci_usb
		for INTF in $BLUETOOTH_INTERFACES ; do
			[ -d /sys/class/bluetooth/$INTF ] || continue
			log "VERBOSE" "`hciconfig $INTF up 2>&1`"
		done

		# For thinkpads, where the thinkpad_acpi driver is loaded,
		# we can handle bluetooth easily
		if [ -f /proc/acpi/ibm/bluetooth ]; then
			log "VERBOSE" "Enabling ThinkPad Bluetooth device"
			echo "enable" > /proc/acpi/ibm/bluetooth
		fi
	else
		log "VERBOSE" "Disabling bluetooth."

		if [ -f /proc/acpi/ibm/bluetooth ]; then
			log "VERBOSE" "Disabling ThinkPad Bluetooth device"
			echo "disable" > /proc/acpi/ibm/bluetooth
		fi

		for INTF in $BLUETOOTH_INTERFACES ; do
			[ -d /sys/class/bluetooth/$INTF ] || continue
			log "VERBOSE" "`hciconfig $INTF down 2>&1`"
		done
		# This may take a while to work because the module may be
		# temporarily in use; we try three times. We don't use rmmod
		# --wait, because that might hang!
		modprobe -rq hci_usb || (sleep 1 ; modprobe -rq hci_usb) || (sleep 1 ; modprobe -rq hci_usb)
	fi
fi

