#!/bin/bash

# Some bluetooth adapters switch to HID mode after the resume from S3/S4.
# This script finds these adapters defined in the @hid_adapter array and
# prepares the script bt_reenable to run hid2hci on the resume.
# (bnc#629455)

. /usr/lib/pm-utils/functions
bt_reenable=/var/run/bluetooth_hid2hci_resume

case "$1" in
    hibernate|suspend)
    echo "looking for hid2hci dongles"
    rm -f $bt_reenable
    hal-find-by-capability --capability 'bluetooth_hci' | perl -e '
    @hid_adapter = ( "413c_8160" );
    @hid_vendor = ( "413c" );
    @hid_product = ( "8162" );
    @hid_model = ( "dell" );
    open F, ">'$bt_reenable'" or die;
    while ( $line = <> ) {
        for ($i = 0; $i < @hid_adapter; $i++) {
            $found = index ($line, $hid_adapter[$i], 0);
            if ($found >= 0) {
               print F "echo hid2hci $hid_model[$i]  $hid_vendor[$i]:$hid_product[$i]; /usr/sbin/hid2hci -v $hid_vendor[$i] -p $hid_product[$i] -m $hid_model[$i]\n";
               last;
               }
            }
        }
        close F;' | sh
        ;;

        thaw|resume)
        echo "reenabling hid2hci dongle if required"
        test -r $bt_reenable && . $bt_reenable
        rm -f $bt_reenable
        ;;

        *)
        ;;
esac

