/* $Id: mntfunc.c,v 1.1.2.2 2002/10/02 14:38:37 armin Exp $
 *
 * Driver for Eicon DIVA Server ISDN cards.
 * Maint module
 *
 * Copyright 2000,2001 by Armin Schindler (mac@melware.de)
 * Copyright 2000,2001 Cytronics & Melware (info@melware.de)
 *
 * This software may be used and distributed according to the terms
 * of the GNU General Public License, incorporated herein by reference.
 */


#include "platform.h"
#include "di_defs.h"
#include "divasync.h"
#include "di_defs.h"
#include "debug_if.h"

extern char *DRIVERRELEASE;

#define DBG_MINIMUM  (DL_LOG + DL_FTL + DL_ERR)
#define DBG_DEFAULT  (DBG_MINIMUM + DL_XLOG + DL_REG)

extern void DIVA_DIDD_Read(void *, int);

#define MAX_DESCRIPTORS  32

static dword notify_handle;
static DESCRIPTOR DAdapter;
static DESCRIPTOR MAdapter;
static DESCRIPTOR MaintDescriptor =
    { IDI_DIMAINT, 0, 0, (IDI_CALL) diva_maint_prtComp };

extern void *diva_os_malloc_tbuffer(unsigned long flags,
				    unsigned long size);
extern void diva_os_free_tbuffer(unsigned long flags, void *ptr);
extern int diva_os_copy_to_user(void *os_handle, void *dst,
				const void *src, int length);
extern int diva_os_copy_from_user(void *os_handle, void *dst,
				  const void *src, int length);

static void no_printf(unsigned char *x, ...)
{
	/* dummy debug function */
}

DIVA_DI_PRINTF dprintf = no_printf;

#include "debuglib.c"

/*
 * stop debug
 */
static void stop_dbg(void)
{
	DbgDeregister();
	memset(&MAdapter, 0, sizeof(MAdapter));
	dprintf = no_printf;
}

/*
 *  DIDD callback function
 */
static void *didd_callback(void *context, DESCRIPTOR * adapter,
			   int removal)
{
	if (adapter->type == IDI_DADAPTER) {
		DBG_ERR(("cb: Change in DAdapter ? Oops ?."));
	} else if (adapter->type == IDI_DIMAINT) {
		if (removal) {
			stop_dbg();
		} else {
			memcpy(&MAdapter, adapter, sizeof(MAdapter));
			dprintf = (DIVA_DI_PRINTF) MAdapter.request;
			DbgRegister("MAINT", DRIVERRELEASE, DBG_DEFAULT);
		}
	} else if ((adapter->type > 0) && (adapter->type < 16)) {
		if (removal) {
			diva_mnt_remove_xdi_adapter(adapter);
		} else {
			diva_mnt_add_xdi_adapter(adapter);
		}
	}
	return (NULL);
}

/*
 * connect to didd
 */
static int DIVA_INIT_FUNCTION connect_didd(void)
{
	int x = 0;
	int dadapter = 0;
	IDI_SYNC_REQ req;
	DESCRIPTOR DIDD_Table[MAX_DESCRIPTORS];

	DIVA_DIDD_Read(DIDD_Table, sizeof(DIDD_Table));

	for (x = 0; x < MAX_DESCRIPTORS; x++) {
		if (DIDD_Table[x].type == IDI_DADAPTER) {	/* DADAPTER found */
			dadapter = 1;
			memcpy(&DAdapter, &DIDD_Table[x], sizeof(DAdapter));
			req.didd_notify.e.Req = 0;
			req.didd_notify.e.Rc =
			    IDI_SYNC_REQ_DIDD_REGISTER_ADAPTER_NOTIFY;
			req.didd_notify.info.callback = didd_callback;
			req.didd_notify.info.context = 0;
			DAdapter.request((ENTITY *) & req);
			if (req.didd_notify.e.Rc != 0xff)
				return (0);
			notify_handle = req.didd_notify.info.handle;
			/* Register MAINT (me) */
			req.didd_add_adapter.e.Req = 0;
			req.didd_add_adapter.e.Rc =
			    IDI_SYNC_REQ_DIDD_ADD_ADAPTER;
			req.didd_add_adapter.info.descriptor =
			    (void *) &MaintDescriptor;
			DAdapter.request((ENTITY *) & req);
			if (req.didd_add_adapter.e.Rc != 0xff)
				return (0);
		} else if ((DIDD_Table[x].type > 0)
			   && (DIDD_Table[x].type < 16)) {
			diva_mnt_add_xdi_adapter(&DIDD_Table[x]);
		}
	}
	return (dadapter);
}

/*
 * disconnect from didd
 */
static void DIVA_EXIT_FUNCTION disconnect_didd(void)
{
	IDI_SYNC_REQ req;

	stop_dbg();

	req.didd_notify.e.Req = 0;
	req.didd_notify.e.Rc = IDI_SYNC_REQ_DIDD_REMOVE_ADAPTER_NOTIFY;
	req.didd_notify.info.handle = notify_handle;
	DAdapter.request((ENTITY *) & req);

	req.didd_remove_adapter.e.Req = 0;
	req.didd_remove_adapter.e.Rc = IDI_SYNC_REQ_DIDD_REMOVE_ADAPTER;
	req.didd_remove_adapter.info.p_request =
	    (IDI_CALL) MaintDescriptor.request;
	DAdapter.request((ENTITY *) & req);
}

/*
 * read/write maint
 */
int maint_read_write(void *buf)
{
	byte data[128];
	dword cmd, id, mask;
	int ret = 0;

	if (diva_os_copy_from_user(NULL, (void *) &data[0],
				   buf, 3 * sizeof(dword))) {
		return (-EFAULT);
	}

	cmd = *(dword *) & data[0];	/* command */
	id = *(dword *) & data[4];	/* driver id */
	mask = *(dword *) & data[8];	/* mask or size */

	switch (cmd) {
	case DITRACE_CMD_GET_DRIVER_INFO:
		if ((ret = diva_get_driver_info(id, data, sizeof(data))) > 0) {
			if (diva_os_copy_to_user
			    (NULL, buf, (void *) &data[0], ret))
				ret = -EFAULT;
		} else {
			ret = -EINVAL;
		}
		break;

	case DITRACE_READ_DRIVER_DBG_MASK:
		if ((ret = diva_get_driver_dbg_mask(id, (byte *) data)) > 0) {
			if (diva_os_copy_to_user
			    (NULL, buf, (void *) &data[0], ret))
				ret = -EFAULT;
		} else {
			ret = -ENODEV;
		}
		break;

	case DITRACE_WRITE_DRIVER_DBG_MASK:
		if ((ret = diva_set_driver_dbg_mask(id, mask)) <= 0) {
			ret = -ENODEV;
		}
		break;

	case DITRACE_READ_TRACE_ENTRY:{
			diva_os_spin_lock_magic_t old_irql;
			word size;
			diva_dbg_entry_head_t *pmsg;
			byte *pbuf;

			if ((pmsg = diva_maint_get_message(&size, &old_irql))) {
				if (size > mask) {
					diva_maint_ack_message(0, &old_irql);
					ret = -EINVAL;
				} else {
					if (!(pbuf = diva_os_malloc_tbuffer(0, size)))
					{
						diva_maint_ack_message(0, &old_irql);
						ret = -ENOMEM;
					} else {
						ret = size;
						memcpy(pbuf, pmsg, size);
						diva_maint_ack_message(1, &old_irql);
						if (diva_os_copy_to_user (NULL, buf,
						     (void *) pbuf, size))
							ret = -EFAULT;
						diva_os_free_tbuffer(0, pbuf);
					}
				}
			} else {
				ret = 0;
			}
		}
		break;

	case DITRACE_READ_TRACE_ENTRYS:{
			diva_os_spin_lock_magic_t old_irql;
			word size;
			diva_dbg_entry_head_t *pmsg;
			byte *pbuf = 0;
			int written = 0;

			if (mask < 4096) {
				ret = -EINVAL;
				break;
			}
			if (!(pbuf = diva_os_malloc_tbuffer(0, mask))) {
				return (-ENOMEM);
			}

			for (;;) {
				if (!(pmsg =
				     diva_maint_get_message(&size, &old_irql))) {
					break;
				}
				if ((size + 8) > mask) {
					diva_maint_ack_message(0, &old_irql);
					break;
				}
				/*
				   Write entry length
				 */
				pbuf[written++] = (byte) size;
				pbuf[written++] = (byte) (size >> 8);
				pbuf[written++] = 0;
				pbuf[written++] = 0;
				/*
				   Write message
				 */
				memcpy(&pbuf[written], pmsg, size);
				diva_maint_ack_message(1, &old_irql);
				written += size;
				mask -= (size + 4);
			}
			pbuf[written++] = 0;
			pbuf[written++] = 0;
			pbuf[written++] = 0;
			pbuf[written++] = 0;

			if (diva_os_copy_to_user(NULL, buf, (void *) pbuf, written)) {
				ret = -EFAULT;
			} else {
				ret = written;
			}
			diva_os_free_tbuffer(0, pbuf);
		}
		break;

	default:
		ret = -EINVAL;
	}
	return (ret);
}

/*
 *  init
 */
int DIVA_INIT_FUNCTION mntfunc_init(int *buffer_length, void **buffer,
				    unsigned long diva_dbg_mem)
{
	if (*buffer_length < 64) {
		*buffer_length = 64;
	}
	if (*buffer_length > 512) {
		*buffer_length = 512;
	}
	*buffer_length *= 1024;

	if (diva_dbg_mem) {
		*buffer = (void *) diva_dbg_mem;
	} else {
		while ((*buffer_length >= (64 * 1024))
		       &&
		       (!(*buffer = diva_os_malloc_tbuffer(0, *buffer_length)))) {
			*buffer_length -= 1024;
		}

		if (!*buffer) {
			DBG_ERR(("init: Can not alloc trace buffer"));
			return (0);
		}
	}

	if (diva_maint_init(*buffer, *buffer_length, (diva_dbg_mem == 0))) {
		if (!diva_dbg_mem) {
			diva_os_free_tbuffer(0, *buffer);
		}
		DBG_ERR(("init: maint init failed"));
		return (0);
	}

	if (!connect_didd()) {
		DBG_ERR(("init: failed to connect to DIDD."));
		diva_maint_finit();
		if (!diva_dbg_mem) {
			diva_os_free_tbuffer(0, *buffer);
		}
		return (0);
	}
	return (1);
}

/*
 *  exit
 */
void DIVA_EXIT_FUNCTION mntfunc_finit(void)
{
	void *buffer;
	int i = 100;

	while (diva_mnt_shutdown_xdi_adapters() && i--) {
		diva_os_sleep(10);
	}

	disconnect_didd();

	if ((buffer = diva_maint_finit())) {
		diva_os_free_tbuffer(0, buffer);
	}
}
