a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * This program is free software; you can redistribute it and/or
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * modify it under the terms of the GNU General Public License as
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * published by the Free Software Foundation; either version 2 of the
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * License, or any later version.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * This program is distributed in the hope that it will be useful, but
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * WITHOUT ANY WARRANTY; without even the implied warranty of
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * General Public License for more details.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * You should have received a copy of the GNU General Public License
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * along with this program; if not, write to the Free Software
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Object interfaces
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/*****************************************************************************
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * The null interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/** Null interface operations */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncstatic struct interface_operation null_intf_op[] = {};
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/** Null interface descriptor */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/** The null interface */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncstruct interface null_intf = INTF_INIT ( null_intf_desc );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/*****************************************************************************
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Object interface plumbing
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Plug an object interface into a new destination object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v intf Object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v dest New destination object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * The reference to the existing destination interface is dropped, a
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * reference to the new destination interface is obtained, and the
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * interface is updated to point to the new destination interface.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Note that there is no "unplug" call; instead you must plug the
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * interface into a null interface.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncvoid intf_plug ( struct interface *intf, struct interface *dest ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync INTF_INTF_DBG ( intf, intf->dest ), INTF_DBG ( dest ) );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Plug two object interfaces together
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v a Object interface A
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v b Object interface B
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Plugs interface A into interface B, and interface B into interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * A. (The basic plug() function is unidirectional; this function is
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * merely a shorthand for two calls to plug(), hence the name.)
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncvoid intf_plug_plug ( struct interface *a, struct interface *b ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Unplug an object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v intf Object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Ignore all further operations on an object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v intf Object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Increment reference count on an object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v intf Object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @ret intf Object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncstruct interface * intf_get ( struct interface *intf ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Decrement reference count on an object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v intf Object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Get pointer to object containing object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v intf Object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @ret object Containing object
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return ( ( ( void * ) intf ) - intf->desc->offset );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Get pass-through interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v intf Object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @ret passthru Pass-through interface, or NULL
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncstatic struct interface * intf_get_passthru ( struct interface *intf ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return ( ( ( void * ) intf ) + desc->passthru_offset );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Get object interface destination and operation method (without pass-through)
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v intf Object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v type Operation type
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @ret dest Destination interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @ret func Implementing method, or NULL
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncvoid * intf_get_dest_op_no_passthru_untyped ( struct interface *intf,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync unsigned int i;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync for ( i = desc->num_op, op = desc->op ; i ; i--, op++ ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Get object interface destination and operation method
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v intf Object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v type Operation type
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @ret dest Destination interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @ret func Implementing method, or NULL
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncvoid * intf_get_dest_op_untyped ( struct interface *intf, void *type,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync while ( 1 ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Search for an implementing method provided by the
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * current destination interface.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync func = intf_get_dest_op_no_passthru_untyped( intf, type, dest );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Pass through to the underlying interface, if applicable */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/*****************************************************************************
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Generic interface operations
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Close an object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v intf Object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v rc Reason for close
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Note that this function merely informs the destination object that
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * the interface is about to be closed; it doesn't actually disconnect
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * the interface. In most cases, you probably want to use
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * intf_shutdown() or intf_restart() instead.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncvoid intf_close ( struct interface *intf, int rc ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " close (%s)\n",
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Default is to ignore intf_close() */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Shut down an object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v intf Object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v rc Reason for close
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Blocks further operations from being received via the interface,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * executes a close operation on the destination interface, and
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * unplugs the interface.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncvoid intf_shutdown ( struct interface *intf, int rc ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync DBGC ( INTF_COL ( intf ), "INTF " INTF_FMT " shutting down (%s)\n",
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Block further operations */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Notify destination of close */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Unplug interface */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Shut down and restart an object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v intf Object interface
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v rc Reason for close
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Shuts down the interface, then unblocks operations that were
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * blocked during shutdown.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncvoid intf_restart ( struct interface *intf, int rc ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Shut down the interface */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync DBGC ( INTF_COL ( intf ), "INTF " INTF_FMT " restarting\n",
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Restore the interface descriptor. Must be done after
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * shutdown (rather than inhibiting intf_shutdown() from
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * nullifying the descriptor) in order to avoid a potential
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * infinite loop as the intf_close() operations on each side
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * of the link call each other recursively.