+#!/bin/sh
+# This file is part of mtetherd
+#
+# Copyright (c) 2010 Gregor Riepl <onitake@gmail.com>
+#
+# Based on osso-usb-mass-storage-enable.sh and osso-usb-mass-storage-disable.sh,
+# Copyright (C) 2004-2009 Nokia Corporation. All rights reserved.
+# Contact: Kimmo Hämäläinen <kimmo.hamalainen@nokia.com>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# version 2 as published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+# 02110-1301 USA
+
+RC=0
+
+/sbin/lsmod | grep g_nokia > /dev/null
+if [ $? = 0 ]; then
+ logger "$0: removing g_nokia"
+
+ initctl emit G_NOKIA_REMOVE
+
+ PNATD_PID=`pidof pnatd`
+ if [ $? = 0 ]; then
+ kill $PNATD_PID
+ else
+ logger "$0: pnatd is not running"
+ fi
+ OBEXD_PID=`pidof obexd`
+ if [ $? = 0 ]; then
+ kill -HUP $OBEXD_PID
+ else
+ logger "$0: obexd is not running"
+ fi
+ SYNCD_PID=`pidof syncd`
+ if [ $? = 0 ]; then
+ kill $SYNCD_PID
+ else
+ logger "$0: syncd is not running"
+ fi
+
+ sleep 2
+ /sbin/rmmod g_nokia
+ if [ $? != 0 ]; then
+ logger "$0: failed to rmmod g_nokia!"
+ exit 1
+ fi
+fi
+
+/sbin/lsmod | grep g_file_storage > /dev/null
+if [ $? = 0 ]; then
+ logger "$0: removing g_file_storage"
+
+ GADGETPATH='/sys/devices/platform/musb_hdrc/gadget'
+ LUN0='gadget-lun0'
+ LUN1='gadget-lun1'
+
+ if [ $# = 0 ]; then
+ # unload all
+ echo "" > $GADGETPATH/$LUN0/file
+ echo "" > $GADGETPATH/$LUN1/file
+ exit 0
+ fi
+
+ # NOTE: works only for 1 or 2 device arguments
+ for lun in $LUN0 $LUN1; do
+ grep $1 $GADGETPATH/$lun/file > /dev/null
+ RC=$?
+
+ if [ $# = 2 ]; then
+ grep $2 $GADGETPATH/$lun/file > /dev/null
+ RC2=$?
+ else
+ RC2=1
+ fi
+
+ if [ $RC = 0 -o $RC2 = 0 ]; then
+ echo "" > $GADGETPATH/$lun/file
+ fi
+ done
+
+ /sbin/rmmod g_file_storage
+ if [ $? != 0 ]; then
+ logger "$0: failed to rmmod g_file_storage!"
+ exit 1
+ fi
+fi
+
+/sbin/lsmod | grep g_ether > /dev/null
+if [ $? != 0 ]; then
+ /sbin/modprobe g_ether
+ RC=$?
+fi
+
+if [ $RC != 0 ]; then
+ logger "$0: failed to install g_ether"
+ exit 1
+fi
+
+exit 0