/*
 * Copyright (c) 1998-2011 Apple Computer, Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */
/*
 * Copyright (c) 1998 Apple Computer, Inc.  All rights reserved.
 *
 * HISTORY
 *
 */

/*
 * Internal definitions used between the iokit user library and
 * server routines.
 */

#ifndef _IOKIT_IOKITSERVER_H
#define _IOKIT_IOKITSERVER_H

#include <IOKit/IOTypes.h>
#include <IOKit/IOKitKeys.h>
#include <IOKit/OSMessageNotification.h>

#ifdef __cplusplus
extern "C" {
#endif
#include <mach/kmod.h>
#ifdef __cplusplus
}
#endif

// IOCatalogueSendData
/*!
 *   @enum IOCatalogueSendData user-client flags.
 *   @constant kIOCatalogAddDrivers  Signals a call to the addDrivers function in IOCatalogue.
 *   @constant kIOCatalogAddDriversNoMatch  Signals a call to the addDrivers function in IOCatalogue but does not start a matching thread.
 *   @constant kIOCatalogRemoveDrivers  Signals a call to the removeDrivers function in IOCatalogue.
 *   @constant kIOCatalogRemoveDriversNoMatch  Signals a call to the removedrivers function in IOCatalogue but does not start a matching thread.
 *   @constant kIOCatalogStartMatching  Signals the IOCatalogue to start an IOService matching thread.
 *   @constant kIOCatalogRemoveKernelLinker  Deprecated; does nothing.
 *   @constant kIOCatalogKextdActive  Signals the kernel that kextd is running.
 *   @constant kIOCatalogKextdFinishedLaunching  Signals the IOCatalogue that kextd has finished sending it information at startup.
 *   @constant kIOCatalogResetDrivers  Resets the IOCatalogue with a new set of personalities.
 *   @constant kIOCatalogResetDriversNoMatch  Resets the IOCatalogue with a new set of personalities but does not start a matching thread.
 */
enum {
	kIOCatalogAddDrivers        = 1,
	kIOCatalogAddDriversNoMatch,
	kIOCatalogRemoveDrivers,
	kIOCatalogRemoveDriversNoMatch,
	kIOCatalogStartMatching__Removed,
	kIOCatalogRemoveKernelLinker__Removed,
	kIOCatalogKextdActive,
	kIOCatalogKextdFinishedLaunching,
	kIOCatalogResetDrivers,
	kIOCatalogResetDriversNoMatch
};

// IOCatalogueGetData
/*!
 *   @enum IOCatalogueGetData user-client flags
 *   @constant kIOCatalogGetContents  Returns a snapshot of the database to the caller.
 */
enum {
	kIOCatalogGetContents         = 1,
	kIOCatalogGetModuleDemandList = 2,
	kIOCatalogGetCacheMissList    = 3,
	kIOCatalogGetROMMkextList     = 4
};

// IOCatalogueReset
/*!
 *   @enum IOCatalogueReset user-client flag
 *   @constant kIOCatalogResetDefault  Removes all entries from IOCatalogue except those used for booting the system.
 */
enum {
	kIOCatalogResetDefault      = 1
};

// IOCatalogueTerminate
/*!
 *   @enum IOCatalogueTerminate user-client flags.
 *   @constant kIOCatalogModuleUnload Terminates all services which depend on a particular module and unloads the module.
 *   @constant kIOCatalogModuleTerminate Terminates all services which depend on a particular module but does not unload the module.
 *   @constant kIOCatalogServiceTerminate Terminates a particular service by name.
 */
enum {
	kIOCatalogModuleUnload      = 1,
	kIOCatalogModuleTerminate,
	kIOCatalogServiceTerminate
};


#ifdef XNU_KERNEL_PRIVATE

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#include <kern/ipc_kobject.h>
#if MACH_KERNEL_PRIVATE
#include <ipc/ipc_port.h>
#endif /* MACH_KERNEL_PRIVATE */

/*
 * Functions in iokit:IOUserClient.cpp
 */

#ifdef __cplusplus
class IOMachPort;
typedef IOMachPort * io_kobject_t;
#else
typedef struct IOMachPort * io_kobject_t;
#endif

extern void iokit_add_reference( io_object_t obj );

extern ipc_port_t iokit_port_make_send_for_object( io_object_t obj,
    ipc_kobject_type_t type );

extern void iokit_ident_no_senders( ipc_port_t port, mach_port_mscount_t mscount );
extern void iokit_object_no_senders( ipc_port_t port, mach_port_mscount_t mscount );
extern void iokit_connect_no_senders( ipc_port_t port, mach_port_mscount_t mscount );
extern void iokit_uext_no_senders( ipc_port_t port, mach_port_mscount_t mscount );

extern kern_return_t
iokit_client_memory_for_type(
	io_object_t     connect,
	unsigned int    type,
	unsigned int *  flags,
	vm_address_t *  address,
	vm_size_t    *  size );

/*
 * Re-externs from <ipc/ipc_port.h> and <kern/ipc_kobject.h> for iokit/...
 *
 * Note: these are safe because IOKitServer.h is used from osfmk/...
 *       context and will fail to build if they diverge.
 */

extern mach_port_t ipc_port_make_send_mqueue(mach_port_t) __result_use_check;
extern mach_port_t ipc_port_copy_send_mqueue(mach_port_t) __result_use_check;
extern void ipc_port_release_send(ipc_port_t port);

extern bool ipc_kobject_is_mscount_current_locked(ipc_port_t port, mach_port_mscount_t mscount);
extern ipc_kobject_t ipc_kobject_get_locked(ipc_port_t port, ipc_kobject_type_t type);
extern void ipc_kobject_enable(ipc_port_t, ipc_kobject_t, ipc_kobject_type_t);
extern ipc_kobject_t ipc_kobject_disable(ipc_port_t, ipc_kobject_type_t);
extern mach_port_t ipc_kobject_make_send(mach_port_t, ipc_kobject_t, ipc_kobject_type_t) __result_use_check;
extern mach_port_t ipc_kobject_copy_send(mach_port_t, ipc_kobject_t, ipc_kobject_type_t) __result_use_check;

/*
 * Functions in osfmk:iokit_rpc.c
 */

extern void iokit_lock_port(ipc_port_t port);
extern void iokit_unlock_port(ipc_port_t port);

extern ipc_port_t iokit_alloc_object_port( io_kobject_t obj,
    ipc_kobject_type_t type );
extern void iokit_destroy_object_port( ipc_port_t port, ipc_kobject_type_t type );

extern ipc_kobject_type_t iokit_port_type(ipc_port_t port);

extern mach_port_name_t iokit_make_send_right( task_t task,
    io_object_t obj, ipc_kobject_type_t type );

extern io_object_t iokit_lookup_io_object(ipc_port_t port, ipc_kobject_type_t type);

extern kern_return_t iokit_mod_send_right( task_t task, mach_port_name_t name, mach_port_delta_t delta );

extern io_object_t iokit_lookup_object_with_port_name(mach_port_name_t name, ipc_kobject_type_t type, task_t task);

extern io_object_t iokit_lookup_connect_ref_current_task(mach_port_name_t name);
extern io_object_t iokit_lookup_uext_ref_current_task(mach_port_name_t name);

extern void iokit_release_port_send( ipc_port_t port );

extern kern_return_t iokit_lookup_raw_current_task(mach_port_name_t name, ipc_kobject_type_t type, ipc_port_t *port);

#ifndef MACH_KERNEL_PRIVATE
typedef struct ipc_kmsg * ipc_kmsg_t;
extern ipc_kmsg_t ipc_kmsg_alloc_uext_reply(size_t);
extern mach_msg_header_t * ikm_header(ipc_kmsg_t);
extern void * ikm_udata_from_header(
	ipc_kmsg_t              kmsg);
#endif /* MACH_KERNEL_PRIVATE */

extern kern_return_t
uext_server(ipc_port_t receiver, ipc_kmsg_t request, ipc_kmsg_t * preply);

extern kern_return_t
iokit_label_dext_task(task_t task);

extern void
iokit_clear_registered_ports(task_t task);

/*
 * Functions imported by iokit:IOMemoryDescriptor.cpp
 */

extern ppnum_t IOGetLastPageNumber(void);

extern kern_return_t IOMapPages(vm_map_t map, mach_vm_address_t va, mach_vm_address_t pa,
    mach_vm_size_t length, unsigned int mapFlags);

extern kern_return_t IOUnmapPages(vm_map_t map, mach_vm_address_t va, mach_vm_size_t length);

extern kern_return_t IOProtectCacheMode(vm_map_t map, mach_vm_address_t va,
    mach_vm_size_t length, unsigned int options);

extern unsigned int IODefaultCacheBits(addr64_t pa);

#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */

#endif /* MACH_KERNEL_PRIVATE */

#endif /* ! _IOKIT_IOKITSERVER_H */
