/*
 *  Routines for the universal switch interface
 *  Copyright (c) by Jaroslav Kysela <perex@jcu.cz>
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "driver.h"
#include "switch.h"

void snd_switch_prepare(snd_kswitch_list_t *list)
{
	list->count = 0;
	list->switches = NULL;
	snd_mutex_prepare(list, lock);
}

snd_kswitch_t *snd_switch_new(snd_kswitch_t * ksw)
{
	snd_kswitch_t *nsw;

	nsw = (snd_kswitch_t *) snd_malloc(sizeof(snd_kswitch_t));
	if (!nsw)
		return NULL;
	memcpy(nsw, ksw, sizeof(snd_kswitch_t));
	return nsw;
}

void snd_switch_free_one(snd_kswitch_t * ksw)
{
	if (ksw) {
		if (ksw->private_free)
			ksw->private_free(ksw->private_data);
		snd_free(ksw, sizeof(snd_kswitch_t));
	}
}

int snd_switch_add(snd_kswitch_list_t *list, snd_kswitch_t *ksw)
{
	snd_kswitch_t **x;

	if (!list || !ksw)
		return -EINVAL;
	snd_mutex_lock(list, lock);
	x = (snd_kswitch_t **)
			snd_malloc((list->count + 1) * sizeof(void *));
	if (!x) {
		snd_mutex_unlock(list, lock);
		return -ENOMEM;
	}
	if (list->switches) {
		memcpy(x, list->switches, (list->count + 1) * sizeof(void *));
		snd_free(list->switches, list->count * sizeof(void *));
	}
	list->switches = x;
	list->switches[list->count++] = ksw;
	snd_mutex_unlock(list, lock);
	return 0;
}

int snd_switch_remove(snd_kswitch_list_t * list, snd_kswitch_t * ksw)
{
	if (!list || !ksw)
		return -EINVAL;
	return -EINVAL;	/* TODO */
}

void snd_switch_free(snd_kswitch_list_t * list)
{
	snd_kswitch_t **x;
	int idx, count;

	if ((x = list->switches) != NULL) {
		count = list->count;
		list->switches = NULL;
		list->count = 0;
		for (idx = 0; idx < count; idx++)
			snd_switch_free_one(x[idx]);
		snd_free(x, count * sizeof(snd_switch_t *));
	} 
	snd_mutex_destroy(list, lock);
}

void snd_switch_lock(snd_kswitch_list_t * list, int xup)
{
	if (!xup) {
		snd_mutex_lock(list, lock);
	} else {
		snd_mutex_unlock(list, lock);
	}
}

#if 0
int snd_switch_list(snd_kswitch_list_t * list, snd_switch_list_t *_list)
{
	snd_switch_list_t xlist;

	xlist.switches = list->count;
	xlist.switches_over = 0;
	memcpy(_list, &xlist, sizeof(*_list));
	return 0;
}
#endif

int snd_switch_read1(snd_kswitch_list_t * list, void *desc, snd_switch_t *_switch)
{
	snd_switch_t sw;
	snd_kswitch_t *ksw, **x;
	int err = -ENOENT, n;

	memcpy(&sw, _switch, sizeof(sw));
	x = list->switches;
	if ( sw.switchn < list->count ) {
		n = sw.switchn;
		memset(&sw, 0, sizeof(sw));
		ksw = x[n];
		sw.switchn = n;
		memcpy(sw.name, ksw->name, sizeof(sw.name));
		err = ksw->get(desc, ksw, &sw);
	}
	if (!err)
		memcpy(_switch, &sw, sizeof(sw));
	return err;	
}

int snd_switch_read(snd_kswitch_list_t * list, void *desc, snd_switch_t *_switch)
{
	int result;

	snd_switch_lock(list, 0);
	result = snd_switch_read1(list, desc, _switch);
	snd_switch_lock(list, 1);
	return result;
}

int snd_switch_write1(snd_kswitch_list_t * list, void *desc, snd_switch_t *_switch)
{
	snd_switch_t sw;
	snd_kswitch_t *ksw, **x;
	int err = -ENOENT;

	memcpy(&sw, _switch, sizeof(sw));
	x = list->switches;
	if ( sw.switchn < list->count ) {
		ksw = x[sw.switchn];
		err = ksw->set(desc, ksw, &sw);
	}
	if (!err)
		memcpy(_switch, &sw, sizeof(sw));
	return err;	
}

int snd_switch_write(snd_kswitch_list_t * list, void *desc, snd_switch_t *_switch)
{
	int result;

	snd_switch_lock(list, 0);
	result = snd_switch_write1(list, desc, _switch);
	snd_switch_lock(list, 1);
	return result;
}

int snd_switch_count(snd_kswitch_list_t * list)
{
	return list->count;
}

int snd_switch_size(snd_kswitch_list_t * list)
{
	return sizeof(unsigned int *) + (list->count * sizeof(snd_switch_t));
}

long snd_switch_store(snd_kswitch_list_t * list, void *desc)
{
	long result = 0;
#if 0	
	int idx;
	snd_switch_t uswitch;
	snd_kswitch_t *sw;

	if (size < sizeof(unsigned int))
		return -EINVAL;
	*((unsigned int *)ptr) = list->count;
	ptr += sizeof(unsigned int);
	result += sizeof(unsigned int);
	size -= sizeof(unsigned int);
	for (idx = 0; idx < list->count; idx++) {
		if (size < sizeof(snd_switch_t))
			return -ENOMEM;
		memset(&uswitch, 0, sizeof(uswitch));
		sw = list->switches[idx];
		strncpy(uswitch.name, sw->name, sizeof(uswitch.name));
		snd_switch_read1(list, desc, &uswitch);
		memcpy(ptr, &uswitch, sizeof(uswitch));
		ptr += sizeof(snd_switch_t);
		result += sizeof(snd_switch_t);
		size -= sizeof(snd_switch_t);
	}
#endif
	return result;
}

long snd_switch_restore(snd_kswitch_list_t * list, void *desc)
{
	long result = 0;
#if 0
	int idx, count;
	snd_switch_t uswitch;

	if (size < sizeof(unsigned int))
		return -EINVAL;
	count = *((unsigned int *)ptr);
	ptr += sizeof(unsigned int);
	size -= sizeof(unsigned int);
	result += sizeof(unsigned int);
	if (count * sizeof(snd_switch_t) < size)
		return -EINVAL;
	for (idx = 0; idx < count; idx++) {
		memcpy(&uswitch, ptr, sizeof(uswitch));
		snd_switch_write1(list, desc, &uswitch);
		ptr += sizeof(snd_switch_t);
		result += sizeof(snd_switch_t);
	}
#endif
	return result;
}
