
/*
 *  linux/atari/stmda.c
 *
 *  Copyright (C) 1994 Roman Hodek
 *
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file README.legal in the main directory of this archive
 * for more details.
 */


/* This file contains some function for controlling the access to the  */
/* ST-DMA chip that may be shared between devices. Currently we have:  */
/*   TT:     Floppy and ACSI bus                                       */
/*   Falcon: Floppy, SCSI and IDE                                      */
/*                                                                     */
/* The controlling functions set up a wait queue for access to the     */
/* ST-DMA chip. Callers to stdma_lock() that cannot granted access are */
/* put onto a queue and waked up later if the owner calls              */
/* stdma_release(). Additionally, the caller gives his interrupt       */
/* service routine to stdma_lock().                                    */

#include <linux/types.h>
#include <linux/sched.h>
#include <linux/atari_stdma.h>
#include <linux/atariints.h>
#include <linux/atarihw.h>


static int		stdma_locked = 0;				/* the semaphore */
static isrfunc	stdma_isr = NULL;				/* int func to be called */
static void		*stdma_isr_data = NULL;			/* data passed to isr */
static struct wait_queue	*stdma_wait = NULL;	/* wait queue for ST-DMA */




/***************************** Prototypes *****************************/

static void stdma_int( struct intframe *fp, void *data );

/************************* End of Prototypes **************************/



/*
 * Function: void stdma_lock( isrfunc isr, void *data )
 *
 * Purpose: Trys to get a lock on the ST-DMA chip that is used by more
 *   then one device driver. Waits on stdma_wait until lock is free.
 *   stdma_lock() may not be called from an interrupt! You have to
 *   get the lock in your main routine and release it when your
 *   request is finished.
 *
 * Inputs: A interupt function that is called until the lock is
 *   released.
 *
 * Returns: nothing
 *
 */

void stdma_lock( isrfunc isr, void *data )
   
{	unsigned long	oldflags;

	save_flags(oldflags);
	cli();		/* protect lock */

	while( stdma_locked )
		/* Since the DMA is used for file system purposes, we have to sleep */
		/* uninterruptible (there may be locked buffers) */
		sleep_on( &stdma_wait );

	stdma_locked   = 1;
	stdma_isr      = isr;
	stdma_isr_data = data;
	restore_flags(oldflags);
}


/*
 * Function: void stdma_release( void )
 *
 * Purpose: Releases the lock on the ST-DMA chip. 
 *
 * Inputs: none
 *
 * Returns: nothing
 *
 */

void stdma_release( void )

{	unsigned long	oldflags;

	save_flags(oldflags);
	cli();
	
	stdma_locked = 0;
	wake_up( &stdma_wait );

	restore_flags(oldflags);
}


/*
 * Function: int stdma_others_waiting( void )
 *
 * Purpose: Check if someone waits for the ST-DMA lock.
 *
 * Inputs: none
 *
 * Returns: 0 if noone is waiting, != 0 otherwise
 *
 */

int stdma_others_waiting( void )

{
	return( stdma_wait != NULL );
}


/*
 * Function: void stdma_init( void )
 *
 * Purpose: Initialize the ST-DMA chip access controlling.
 *   It sets up the interrupt and its service routine.
 *
 * Inputs: none
 *
 * Return: nothing
 *
 */

void stdma_init( void )

{
	stdma_isr = NULL;
	add_isr( IRQ_MFP_FDC, stdma_int, 0, NULL );
	mfp.int_en_b |= 0x80;	/* enable int */
	mfp.int_mk_b |= 0x80;	/* not masked */
}


/*
 * Function: void stdma_int()
 *
 * Purpose: The interupt routine for the ST-DMA. It calls the isr
 *   registered by stdma_lock().
 *
 */

static void stdma_int( struct intframe *fp, void *data )

{
	if (stdma_isr)
		(*stdma_isr)( fp, stdma_isr_data );
}
