<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def disconnect(self):
 """Disconnect the device."""

        self.chain.connection.log("Disconnecting: {}".format(self))
        if self.connected:
            if self.protocol:
                if self.is_console:
                    while self.mode != 'global':
                        try:
                            self.send('exit', timeout=10)
                        except CommandTimeoutError:
                            break

                self.protocol.disconnect(self.driver)
                self.protocol = None

            self.connected = False
            self.ctrl = None